最近写了一个Cookie的login.jsp文件,判断用户是否第一次登录而会有不同的结果,老是调试不成功,没办法了,大家可不可以帮我调试一下是哪里错了,代码如下:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%
//起始变量
String UserName="1",UserEMail="2",LoginDate="3";
Cookie temp=null;
//取得Cookie变量数组
Cookie[] cookies=request.getCookies();
//取得Cookie的变量组的长度
int cookielen=cookies.length;
//利用for循环从kookie中取得资料
for(int i=0;i<cookielen;i++)
{
    temp=cookies[i];
//判断是否取得Cookie中的资料
if(temp.getName().equals("Name"))
{
   //下面的叙述将把资料设置给变量
   UserName=temp.getValue();
}
else if(temp.getName().equals("E_Mail"))
{
   //下面的叙述将把资料设置给变量
   UserEMail=temp.getValue();
}
else if(temp.getName().equals("LoginDate"))
{
   //下面的叙述将把资料设置给变量
   LoginDate=temp.getValue();
}
}
int tmplen=LoginDate.length();
boolean FirstLogin;
if(tmplen!=0)
{
  //设置不是第一次登录
  FirstLogin=false;
}
else
{
   //预设为true,代表第一次浏览网页
   FirstLogin=true;
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Cookie的用法</title>
</head><body>
 <h2 align="center">Cookie有效时间的设置
</h2>
 <hr>
 <%
 if(FirstLogin==true)
 {
 %>
 <p>首度光临的朋友,请输入你的姓名与E_Mail登录</p>
 <%
  }
else{ 
 %>
 <p>欢迎再度光临,你曾于<%=LoginDate%>拜访过本站</p>
 <% } %>
 <form name="form1" method="post" action="settime.jsp">
   <p>姓名:
     <input name="textfield" type="text" value="<%=UserName%>" size="20">
   </p>
   <p>E_Mail:
     <input name="textfield2" type="text" value="<%=UserEMail%>" size="40">
</p>
   <p>
     <input type="submit" name="Submit" value="提交">
</p>
 </form>
 <p>&nbsp;</p>
</body>
</html>

解决方案 »

  1.   

    这个文件没问题,是编译器的问题,现在调试无误了,这是关于cookie的应用实例,初学者也可以参考参考
      

  2.   

    此文件还会出现中文问题,现在介绍补救方法如下
    //写入cookie
    realname=java.net.URLEncoder.encode(realname, "UTF-8");
    Cookie cookie=new Cookie("realname",realname);
    response.addCookie(cookie);//读出cookie
    String realname1=cookis[0].getValue();
    realname1=java.net.URLDecoder.decode(realname, "UTF-8");
    out.print(realname1);