日期:2014-05-17  浏览次数:20732 次

Cookie使用时候出现了错误、求解啊
下面是我在写网页时候JSP的一段代码,cookie使用有错误额[code=Java][/code]
<%
  String value="user";
  Cookie[] ck = request.getCookies();
  if( ck==null/*session.getValue("user")== null*/)
  {
%>
<%
}
else
  for(Cookie cookie:ck)//这行显示错误为什么啊??
  {
  if(cookie.getName().equals("user")) {
  value=cookie.getValue();
   
  break;
  }
  }
  {
%>
//完整的源代码是:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<style type="text/css">
<!--
.a1 {
font-size: 12px;
}
-->
</style>
<%
  String userName = request.getParameter("userName");
  String password = request.getParameter("password");
  if ("Lucy".equals(userName) && "123".equals(password))
  {
//session.setAttribute("user",userName);
Cookie newCookie= new Cookie("user",userName);
  newCookie.setMaxAge(90);
  newCookie.setPath("/");
  response.addCookie(newCookie);
}
  else
  {
//session.setAttribute("user",null);
Cookie newCookie= new Cookie("user",null);
  newCookie.setMaxAge(90);
  newCookie.setPath("/");
  response.addCookie(newCookie);
  }
%>
<%
  String value="user";
  Cookie[] ck = request.getCookies();
  if( ck==null/*session.getValue("user")== null*/)
  {
%>
<form action="index.jsp" method="post" enctype="multipart/form-data" name="form1">
  <table width="205" height="77" border="0" cellpadding="0" cellspacing="0">
  <tr>
  <td colspan="2" align="center" class="a1">会员登录</td>
  </tr>
  <tr>
  <td width="77" class="a1">用户名:</td>
  <td width="112" class="a1">
  <label>
  <input name="userName" type="text" id="textfield" size="16">
  </label>
  </td>
  </tr>
  <tr>
  <td class="a1">密码:</td>
  <td class="a1">
  <input name="password" type="password" id="textfield2" size="16">
  </td>
  </tr>
  <tr>
  <td height="19" align="center">&nbsp;</td>
  <td height="19" align="left" class="a1">
  <input type="submit" name="button" id="button" value="提交">
  <input type="submit" name="button2" id="button2" value="注册">
  </td>
  </tr>
  </table>
  </form>
<%
}
else
  for(Cookie cookie:ck)
  {