0

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?

Vikas V
  • 3,176
  • 2
  • 37
  • 60

2 Answers2

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have 2 JSP forms(form1 & form2). Rendering of these forms is decided based on Cookie value. When User remains idle for (say) 15mins, session is getting destroyed, other values are getting flushed but Cookie is not getting cleared and am not able to redirect to the correct form. It is instead rendering the other incorrect form. Hence I thought of clearing cookies in `sessionDestroyed` method. – Vikas V Mar 28 '13 at 13:44
  • Just make it a session cookie (set `maxAge` to `-1`) so that it get automatically destroyed by end of session. – BalusC Mar 28 '13 at 13:46
0

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.

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50