1

there is a way to check if there is an active session directly in jsf page? I have try this but it doesn't work:

<p:ajaxStatus onerror="#{session == null ? 'idleDialog.show();' : null}"

thank you in advance

@Update I have see that onerror isn't fired even if viewExpiredException occurr.

Roberto de Santis
  • 868
  • 5
  • 15
  • 26
  • 1
    Related question: http://stackoverflow.com/questions/2319020/mvc-with-jquery-handling-session-expire – BalusC Jan 16 '11 at 16:56
  • As per your update, are you using a PrimeFaces release candidate instead of final version? 2.2 RC1 has a bug: http://code.google.com/p/primefaces/issues/detail?id=1471 This is a different story. – BalusC Jan 16 '11 at 17:08

2 Answers2

3

There is always an active session at the time the EL is evaluated. If there isn't before the page was opened, it is automatically created.

In JSF expiring sessions are a problem because of the so called "state saving method". By default the state of the generated page is stored in the session. If you try to submit the form and the session is expired, the state is lost and hence an error occurs.

But even in that case a new session is generated. So, as BalusC noted, you can keep the session alive - poll with ajax requests (for example richfaces has such facilities) so that the session never expires

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • yes i know that, but i need to check if session was expired and so open a dialog for restore the session. – Roberto de Santis Jan 16 '11 at 15:05
  • 1
    How would you like to restore the session if it was expired? You'd rather like to either keep the session alive as long as the user is active on the page, or to redirect to an error/login page immediately when the session expires. – BalusC Jan 16 '11 at 15:10
  • I may create a new session simply showing an h:button in the dialog, so the user click on it and this generate a new session. @Bozho yes using ajax. the in web.xml doesn't wok – Roberto de Santis Jan 16 '11 at 15:13
  • a new session is generated automatically when you open a new page, or refresh the current page. So you can simply refresh, if there isn't anything of interest stored in the session. – Bozho Jan 16 '11 at 15:14
  • My original question is how to check if session is expired directly in jsf page, maybe using #{session == null} or something like that – Roberto de Santis Jan 16 '11 at 15:18
  • as I said - when this EL is evaluated the session is never expired. It may expire later. – Bozho Jan 16 '11 at 15:19
  • so there isn't a way to check if session is expired directly in jsf page it's right? – Roberto de Santis Jan 16 '11 at 15:21
  • the session can't possibly be expired at that point. See my update for a bit of clarification. – Bozho Jan 16 '11 at 15:22
  • Ok, your solution may work, but this solution break session goal that is also a security goal. So i'm finding a solution to check if the session is expired and if so, show a dialog with a button that redirect you to login page. – Roberto de Santis Jan 16 '11 at 15:34
  • ``the session can't possibly be expired at that point`` - I think this can be done. Just execute something like HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false); if (session != null) { session.invalidate(); } in the backing bean prior to rendering, or in the midst of rendering and the session will actually not be there. – Arjan Tijms Jan 16 '11 at 16:18
0

You can use the following EL expression in JSF 2.0:

#{facesContext.externalContext.getSession(false) == null}

Do note that referencing the implicit EL object session will automatically (re)create it again. This one can thus never be used for testing.

E.g.

Suppose you would have invalidated the session in a backing bean, and a Facelet with the following content was rendered:

#{facesContext.externalContext.getSession(false) == null}
#{session.new}
#{facesContext.externalContext.getSession(false) == null}

This would render:

true true false

The session is first really gone, but by referencing the implicit EL object it's created again (and will be in the new state). The second test then finds the session to be there again.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • 2
    While technically correct, this does not answer OP's initial functional requirement of displaying an JS alert/dialog whenever the session has been expired while the page is still open in the browser. Your answer assumes that the client has got to manually fire a brand new synchronous HTTP request itself after the session expire (and that there are no session/view scoped managed beans bound to the page). I must however admit that the OP indeed didn't express that clearly enough in the question. – BalusC Jan 16 '11 at 16:50
  • Yes, you are right. I focussed solely on the title question "How to check if there is an active session in a JSF page?". To achieve what the OP want on a functional level is a bit more involved. I'm thinking along the lines of an async Servlet 3.0 suspended request, that gets resumed when the session expired event fires. I've no experience with this myself yet, so I don't feel comfortably providing that as an answer. – Arjan Tijms Jan 16 '11 at 17:32