当我使http会话无效时,它会抛出一个类似下面的空指针异常:
2012年12月14日下午9:49:52 org.apache.catalina.session.StandardSession到期 严重:会话事件监听器引发异常 显示java.lang.NullPointerException
建议。
答案 0 :(得分:1)
在代码中加入一些断点,并在DEBUG模式下使用您喜欢的IDE来确认此“会话”引用变量是否指向空对象。
然后,只需查看文档以阐明getSession()方法的所有细节(它可能会给你一些想法):http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletRequest.html#getSession%28boolean%29
答案 1 :(得分:0)
我认为你的问题会解决,请尝试一下。作者:Gourav Gavate public void doGet(HttpServletRequest request,HttpServletResponse response) 抛出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");
}
}