How to effectively destroy 'session' in Java Servlet?

前端 未结 2 1300
遇见更好的自我
遇见更好的自我 2020-12-28 17:11

The Servlet I\'m working has a variable session.

I\'ve tried session.invalidate();, this seem to have destroyed session but when I do a red

相关标签:
2条回答
  • 2020-12-28 17:48

    You need to return from the method after sending the redirect.

    if (request.getParameter("logout") != null) {  
        session.invalidate();
        response.sendRedirect("restanes.jsp");
        return; // <--- Here.
    }
    

    Otherwise the code will continue to run and hit some session.getAttribute() method further down in the block causing exactly this exception. At least, that's the most likely cause of the problem described so far and based on the fact that this is a pretty common starter's mistake. See also e.g. this answer.

    0 讨论(0)
  • 2020-12-28 18:06

    Your code is okay

    if(request.getParameter("logout") != null )
    {  
      session.invalidate();
      response.sendRedirect("restanes.jsp");
    }
    

    but make sure the redirecting page does not contain any session attributes. 500 internal error coming from "restanes.jsp" page. work out with the redirected page and session activity.

    0 讨论(0)
提交回复
热议问题