如何在Java Servlet中有效地销毁“会话”?

时间:2021-08-25 19:45:38

The Servlet I'm working has a variable session.

我正在处理的Servlet有一个可变会话。

I've tried session.invalidate();, this seem to have destroyed session but when I do a redirect like so response.sendRedirect("restanes.jsp");, it gives me HTTP Status 500 error with this line:

我尝试过session.invalidate();,这似乎已经破坏了会话,但是当我执行像so response.sendRedirect(“restanes.jsp”)这样的重定向时,这一行会给我带来HTTP Status 500错误:

java.lang.IllegalStateException: getAttribute: Session already invalidated

This is expected since I was trying to destroy the session.

这是预料之中的,因为我试图破坏会议。

But why is the page unable to redirect? On the same page elsewhere I've redirected successfully.

但是为什么页面无法重定向?在同一页上,我已成功地重定向。

How can I destroy session and redirect successfully?

如何销毁会话并成功重定向?

Code snippet:

代码片段:

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

Update: All I needed to do was return; after response.sendRedirect("restanes.jsp");. Sincere thanks to BalusC.

更新:我只需要返回;后response.sendRedirect(“restanes.jsp”);。衷心感谢BalusC。

2 个解决方案

#1


32  

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.

否则,代码将继续运行,并在代码块的更下方单击session.getAttribute()方法,导致此异常。至少,这是目前为止描述的问题的最可能的原因,并且基于这是一个非常常见的启动错误的事实。参见这个答案。

#2


0  

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.

但是要确保重定向页面不包含任何会话属性。来自“restanes”的500个内部错误。jsp页面”。使用重定向的页面和会话活动。

#1


32  

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.

否则,代码将继续运行,并在代码块的更下方单击session.getAttribute()方法,导致此异常。至少,这是目前为止描述的问题的最可能的原因,并且基于这是一个非常常见的启动错误的事实。参见这个答案。

#2


0  

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.

但是要确保重定向页面不包含任何会话属性。来自“restanes”的500个内部错误。jsp页面”。使用重定向的页面和会话活动。