当我使 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 public void doGet(HttpServletRequest request, HttpServletResponse response) throws 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");
}
}