[英]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.