Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling 'session expired' in JSF web application, running in JBoss AS 5 [duplicate]

This question is related to my other question "How to redirect to Login page when Session is expired in Java web application?". Below is what I'm trying to do:

  1. I've a JSF web application running on JBoss AS 5
  2. When the user is inactive for, say 15 minutes, I need to log out the user and redirect him to the login page, if he is trying to use the application after the session has expired.
  3. So, as suggested in 'JSF Logout and Redirect', I've implemented a filter which checks for the session expired condition and redirects the user to a session-timed-out.jsp page, if the session has expired.
  4. I've added SessionExpiryCheckFilter on top of all other filter definitions in web.xml, so that my session expiry check will get the first hit always.

Now comes the challenge I'm facing. Since I'm using JBoss AS, when the session expired, JBoss automatically redirects me to the login page (note that the session expiry check filter is not invoked). So, after I log-in, my SessionExpiryCheckFilter intercepts the request, and it sees a session is available. But, it throws the exception javax.faces.application.ViewExpiredException: viewId:/mypage.faces - View /mypage.faces could not be restored.

Have anyone faced this issue before? Any ideas to solve this issue?

like image 516
Veera Avatar asked Jun 23 '09 06:06

Veera


People also ask

How to solve session timeout problem in Java?

You can use scheduler to forcefully destroy session after 30 minute if you store session creation time. You can start a scheduler on application start up and it will check the sessions which are older than 30 minute will be invalidated.

How to write code for session timeout in Java?

HttpSession session = request. getSession(); session. setMaxInactiveInterval(10*60);


2 Answers

The following approach works for me. Note that you have to use the JSTL core taglib redirect and not the jsp redirect in order for this to work (as the jsp also expires).

In your FacesConfig.xml you put the following:

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/sessionExpired.jsf</location>
</error-page>

sessionExpired.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:redirect url="/login.jsf" />

You can also use this approach for other error types or exceptions. For example the element contains a mapping between an error code or exception type and the path of a resource in the web application.:

<error-page>
    <error-code>400</error-code>
    <location>/400.html</location>
</error-page>

or element contains a fully qualified class name of a Java exception type.

<error-page>
    <exception-type>javax.servlet.ServletException</exception-type>
    <location>/servlet/ErrorDisplay</location>
</error-page>
like image 113
Chris Dale Avatar answered Oct 22 '22 06:10

Chris Dale


If you are using Mojarra/Sun RI you might want to try to add this to your web.xml:

<context-param>
    <param-name>com.sun.faces.enableRestoreView11Compatibility</param-name> 
    <param-value>true</param-value>
</context-param>

However be aware that this isn't always the perfect solution. It hides the fact that the user has lost its session.

like image 23
skytteren Avatar answered Oct 22 '22 06:10

skytteren