Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect session has been invalidated in JSF 2?

Tags:

session

jsf

jsf-2

In my application I have a quit button, on clicking of which the session for the current user is invalidated by the following piece of the code..

FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

And I redirect the user to a different page.

But now I want if user click on the back button I will take him to the start page of the application instead of the last page visted by him. I have an application phase listener which sets the page cache related headers to 'none', now all I want is to detect that for that user session has been invalidated. But I guess whenever the user is clicking the back button it is creating a new session for the user. Is there any way to prevent it?

like image 513
user1220373 Avatar asked Dec 16 '22 22:12

user1220373


1 Answers

How to detect session has been invalidated in JSF 2?

Check if the user has requested a session ID which is not valid.

HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid()) {
    // Session has been invalidated during the previous request.
}

it is creating a new session for the user. Is there any way to prevent it?

Just don't let your application code create the session then. The session will implicitly be created when your application needs to store something in the session, e.g. view or session scoped beans, or the view state of a <h:form>, etc.


I have an application phase listener which sets the page cache related headers to 'none'

A servlet filter is a better place for this. See also Avoid back button on JSF web application

like image 69
BalusC Avatar answered Dec 23 '22 08:12

BalusC