[英]session.invalidate() threw null pointer exception
当我使http会话无效时,它引发了一个空指针异常,如下所示:
2012年12月14日晚上9:49:52 org.apache.catalina.session.StandardSession过期严重:会话事件侦听器引发异常java.lang.NullPointerException
建议。
在代码中放置一些断点,并在调试模式下使用喜欢的IDE来确认此“会话”引用变量是否指向空对象。
然后,只需检查文档以弄清围绕getSession()方法的所有细节(它可能会给您一些想法): http : //tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/ HttpServletRequest.html#getSession%28boolean%29
我认为使用此方法可以解决您的问题,请尝试一下。 由Gourav Gavate公共void doGet(HttpServletRequest请求,HttpServletResponse响应)引发ServletException,IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String op = request.getParameter("operation");
System.out.println("operation valuse is ->"+op);
LoginService service = null;
ResultSet rs= null;
HttpSession session=null;
if (op.equals("Login")){
//System.out.println("1");
String un=request.getParameter("username");
String psd=request.getParameter("password");
try {
rs = service.searchByUsernameandPassword(un, psd);
//System.out.println("2");
while (rs.next()) {
//System.out.print(rp.getInt(1));
//System.out.print("\t"+rs.getString(2));
// System.out.println("in while");
if(un.equals(rs.getString(3))&& psd.equals(rs.getString(4)))
{
// System.out.println("in if");
session=request.getSession(true);
String name = rs.getString(1)+""+rs.getString(2);
session.setAttribute("name",name);
// System.out.println("set into session");
response.sendRedirect("http://localhost:8080/Registration/Registration.jsp");
}else{
response.sendRedirect("http://localhost:8080/Registration/Login.jsp");
}
}
} catch (Exception e4) {
System.out.println(e4);
}
} else if (op.equals("Logout")){
/*System.out.println("Into logout");
if(session!=null){
System.out.println("Into logout if");
session.removeAttribute("name");
session.invalidate();
response.sendRedirect("http://localhost:8080/Registration/Login.jsp");
}else{
System.out.println("Into logout else");
// session.removeAttribute("name");
session.invalidate();
response.sendRedirect("http://localhost:8080/Registration/Login.jsp");
}
*/
HttpSession session1 = request.getSession();
//session.removeAttribute("name");
session1.invalidate();
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires", 0);
response.sendRedirect("http://localhost:8080/Registration/Login.jsp");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.