当前位置:编程学习 > JAVA >>

JSP问题

<%@ 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 14

11:  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()) {

--------------------编程问答-------------------- 将你的所有内容放在一个form里面,<form>内容</form> --------------------编程问答--------------------
引用 1 楼 jennis1990 的回复:
将你的所有内容放在一个form里面,<form>内容</form>


这方法不行。。 --------------------编程问答-------------------- 不用加<form>,但是你要先运行一次,别关掉浏览器,在运行一次,就可以了 --------------------编程问答--------------------
引用 3 楼 jennis1990 的回复:
不用加<form>,但是你要先运行一次,别关掉浏览器,在运行一次,就可以了


根本一定用都没有 --------------------编程问答-------------------- 我的运行了
第一次:
HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception 
org.apache.jasper.JasperException: java.lang.NullPointerException

root cause 
java.lang.NullPointerException

note 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时候,里面没有定西,会报错。。 --------------------编程问答-------------------- 楼上正解 --------------------编程问答-------------------- 把cookie 打印出来,看看是不是为空
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,