在response中使用Cookie,求助!
<%@page import= "javax.servlet.http.Cookie.java.util.* "%>
<%
String username= "liuning "
Cookie[] cookie=request.getCookies();
Cookie cookie_response=null;
List list=Arrays.asList(cookie);//query List,Attays
Iterator it=list.iterator();//query Iterator
while(it.hasNext())
{
Cookie temp=(Cookie)it.next();
if(temp.getName().equals(userName+ "_access_time "))
{
cooke_response=temp;
break;
}
}
out.println( "now "+new java.util.Date()+ " <br> ");
if(cookie_response!=null)
{
out.println( "last time you visited: "+cookie_response.getValue());
cookie_response.setValue(new Date().toString());
}
else
{
cooke_response=new Cookie(username+ "_access_time ",new java.util.Date().toString());
}
response.addCookie(cookie_response);
response.setContentType( "test/heml ");
response.flushBuffer();
%>
报错:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /response.jsp:6
3: String username= "liuning ";
4: Cookie[] cookie=request.getCookies();
5: Cookie cookie_response=null;
6: List list=Arrays.asList(cookie);//query List,Attays
7: Iterator it=list.iterator();//query Iterator
8: while(it.hasNext())
9: {
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handle
JspException(JspServletWrapper.java:506)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:395)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException java.util.Arrays$ArrayList. <init> (Arrays.java:3357)
java.util.Arrays.asList(Arrays.java:3343)
org.apache.jsp.response_jsp._jspService(response_jsp.java:50)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.
请高手赐教!
------解决方案------------