2

I'm new to JSF and I was doing some research about Scopes and Http session lifecycles, but one thing was not clear to me.

I know that is possible to store variables using sessionMap from ExternalContext, and it used to work very fine for what I needed. I also know that when the session is invalidated all the data stored on the map is lost.

However, what I don't know is: when the page is refreshed the session is invalidated?

My problem appeared when I had to put a download request on one of the buttons from my web application. Apparently download requests cannot be made via Ajax, so the entire page have to be refreshed. The download proceed normaly, but after that, all the data stored on the map is gone, including all the managed beans. The user data itself is not that important as I can store it and then put it again on the new session map. But what about the managed beans? How should I proceed?

Tinadm
  • 359
  • 3
  • 16
  • 1
    *when the page is refreshed the session is invalidated* - This is not normal behaviour. Please track the HTTP traffic and tell if it's the browser who forgot to send the session cookie, or if it's the server who returned a new session cookie. This way we can determine who's at fault: the client or the server. – BalusC Oct 22 '12 at 19:58
  • @BalusC first of all, thanks for the answer. Well, I'm not completly sure if the session is invalidated, but if I the sessionMap is lost and a new session is created, I guess I can affirm that, right? About your suggestion, would mind giving me a hint about how I would do that? I mean, track the cookies and everything? – Tinadm Oct 22 '12 at 20:19
  • 1
    Press F12 in Chrome/IE9/Firebug and check *Network* tab. To learn how basic session concept works, head to this answer http://stackoverflow.com/a/3106909 – BalusC Oct 22 '12 at 20:21
  • Ok, I'll try that and then post here what I find out. Thanks for the help. – Tinadm Oct 22 '12 at 20:35
  • @BalusC I did as you said. Checking network and cookie tabs from firebug, I saw that after the download request, witch refreshes the page, my cookies are gone. Do you know why is that happening? – Tinadm Oct 23 '12 at 14:21
  • 1
    Please pay attention to `Set-Cookie` header in the response from the server and the `Cookie` header in the request to the server. What happened here? Did the server after the download send a new `Set-Cookie` header which instructs the browser to expire the cookies? – BalusC Oct 23 '12 at 14:33
  • @BalusC Yes. After I send the download request, a new cookie is set. The browser sends the old cookie, and on the reply, the server sends a new cookie. – Tinadm Oct 23 '12 at 14:53
  • Okay. Create a `HttpSessionListener` and put a breakpoint on `sessionDestroyed()` and check the call stack who initiated it and why. – BalusC Oct 23 '12 at 14:59
  • That was an excellent suggestion! I found out that another bean was somehow invalidating the session. I guess now I will be able to fix this. Thanks! – Tinadm Oct 23 '12 at 15:09
  • Great. I reposted it as an answer. – BalusC Oct 23 '12 at 15:12
  • Too bad I don't have enough reputation to up vote your answer /: – Tinadm Oct 23 '12 at 15:14

1 Answers1

2

Assuming that it's not the webbrowser who misbehaved, this can only happen if the server side code is actually by itself invalidating the session by calling ExternalContext#invalidateSession() or HttpSession#invalidate().

If you can't seem to nail it down, then create a HttpSessionListener and put a debug breakpoint on sessionDestroyed() method and investigate the call stack who initiated it and why.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555