<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="javax.servlet.http.Cookie, java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String userName = "hellking";
Cookie[] cookie = request.getCookies();
Cookie cookie_response = null;
List list = Arrays.asList(cookie);

Iterator it = list.iterator();
while(it.hasNext()) {
Cookie temp = (Cookie)it.next();
if(temp.getName().equals(userName + "_access_time")) {
cookie_response = temp;
break;
}
}

out.println("当前的时间:" + new java.util.Date() + "<br />");
if(cookie_response != null) {
out.println("上一次访问的时间:" + cookie_response.getValue());
cookie_response.setValue(new Date().toString());
}
else
cookie_response = new Cookie(userName+"_access_time", new java.util.Date().toString());

response.addCookie(cookie_response);
response.setContentType("text/html");
response.flushBuffer();
%>
</body>
</html>为什么运行显示:
org.apache.jasper.JasperException: An exception occurred processing JSP page /vwe.jsp at line 1411:  String userName = "hellking";
12:  Cookie[] cookie = request.getCookies();
13:  Cookie cookie_response = null;
14:  List list = Arrays.asList(cookie);
15: 
16:  Iterator it = list.iterator();
17:  while(it.hasNext()) {

解决方案 »

  1.   

    将你的所有内容放在一个form里面,<form>内容</form>
      

  2.   

    不用加<form>,但是你要先运行一次,别关掉浏览器,在运行一次,就可以了
      

  3.   

    我的运行了
    第一次:
    HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessagedescriptionThe server encountered an internal error () that prevented it from fulfilling this request.exception 
    org.apache.jasper.JasperException: java.lang.NullPointerExceptionroot cause 
    java.lang.NullPointerExceptionnote The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.1 logs.第二次运行:
    当前的时间:Mon Dec 26 22:44:21 CST 2011第三次运行:
    当前的时间:Mon Dec 26 22:44:31 CST 2011
     上一次访问的时间:Mon Dec 26 22:44:21 CST 2011 第四次:
    当前的时间:Mon Dec 26 22:44:49 CST 2011
     上一次访问的时间:Mon Dec 26 22:44:31 CST 2011 
    估计是第一次访问cookie时候,里面没有定西,会报错。。
      

  4.   

    把cookie 打印出来,看看是不是为空