How to get request object in sessionDestroyed method?

北战南征 提交于 2019-12-25 01:51:12

问题


I am in need of Request object in sessionDestroyed method because I need to retrieve Cookies there.

public void sessionDestroyed(HttpSessionEvent httpSessionEvent) 
{
// Here I need to get Request object so that I can retrieve Cookies.
}

Is it possible to get request object so that I can retrieve Cookies there?


回答1:


No. There is not necessarily means of a HTTP request when the session is been destroyed. It can namely be destroyed during a timeout because the client hasn't sent any request for e.g. 30 minutes. The only case that a HTTP request would be available, is the case when you explicitly invoke HttpSession#invalidate() yourself when e.g. a logout button is been pressed. But at the moment when you invoke that method, you would already have a HTTP request at your hands. You could then just do the cookie job at the same moment instead of in a session listener.

You're not clear on the concrete functional requirement for which you incorrectly thought that this would be the right solution, so I can't give a well suited answer as to how to solve that properly. But one of the ways would be to just store a copy of the information stored in the cookie as an attribute of the session. If you need to do that on a per-request basis, because the cookie value could be manipulated by JS in the client side, for example, then you could use a servlet filter for this.




回答2:


No. No Way.

Session Destroy is something which might happen asynchronously and outside the scope of request life cycle.

It is not correct in your thinking to get the reference to the request object outside the scope of request life cycle.



来源:https://stackoverflow.com/questions/15682914/how-to-get-request-object-in-sessiondestroyed-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!