这段登陆验证的代码说cookie值传不过,怎么原因?
package com.bean;
//checkLogin.java
//import required classes
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//class checkLogin
public class checkLogin
{
public String username = "";
public boolean check(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
String cookieName = "username";
Cookie[] myCookies = req.getCookies();
this.username = this.getCookieValue(myCookies,cookieName,"not found");
PrintWriter out = res.getWriter();
if(!this.username.equals("not found"))
{
//out.println("早上好," + this.username + "!");
return true;
}else{
out.println("登陆失败!");
return false;
}
}
public String getUserName()
{
return this.username;
}
public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue)
{
int count = cookies.length;
for(int i=0;i<count;i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
}
报错:
信息: JK: ajp13 listening on /0.0.0.0:8009
2007-8-9 14:28:32 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/281 config=null
2007-8-9 14:28:32 org.apache.catalina.storeconfig.StoreLoader load
信息: Find registry server-registry.xml at classpath resource
2007-8-9 14:28:33 org.apache.catalina.startup.Catalina start
信息: Server startup in 9578 ms
2007-8-9 14:28:49 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at com.bean.checkLogin.getCookieValue(checkLogin.java:40)
at com.bean.checkLogin.check(checkLogin.java:19)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:62)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)