title: Redirecting after HttpSession time out
body: <p>I have been looking at many posts on this topic but couldn't get a solution that works in my case.</p>
  
  <p>I am using Java EE 6 with JSF 2.0 (deployed on JBoss AS 7.1)</p>
  
  <p>In my <code>web.xml</code> I have:</p>
  
  <pre><code>    &lt;session-config&gt;
          &lt;session-timeout&gt;1&lt;/session-timeout&gt;
      &lt;/session-config&gt;
  </code></pre>
  
  <p>and I <strong>want the user to be redirected to the login page when the session automatically times out.</strong></p>
  
  <p>what I have tried:</p>
  
  <p><strong>Approach 1: using filter</strong></p>
  
  <p>I have tried the following filter:</p>
  
  <pre><code>@WebFilter()
  public class TimeOutFilter implements Filter {
  
          @Override
          public void init(FilterConfig filterConfig) throws ServletException { 
          }
  
          @Override
          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
          ServletException {
          System.out.println("filter called");
          final HttpServletRequest req = (HttpServletRequest) request;
          final HttpSession session = req.getSession(false);
          if (session != null &amp;&amp; !session.isNew()) {
              chain.doFilter(request, response);
          } else {
              System.out.println("Has timed out");
              req.getRequestDispatcher("/logon.xthml").forward(request, response);
          }
      }
  
      @Override
      public void destroy() {
      }
  }
  </code></pre>
  
  <p>In the <code>web.xml</code> I have tried</p>
  
  <pre><code>&lt;filter-mapping&gt;
      &lt;filter-name&gt;TimeOutFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&lt;filter-mapping&gt;
      &lt;filter-name&gt;TimeOutFilter&lt;/filter-name&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>The filter works as it is called on every request (logging of "fiter called" in the console). <strong>However it is not called when the session times out.</strong></p>
  
  <p><strong>Approach 2: HttpSessionLister</strong></p>
  
  <p>I have tried to use a <code>HttpSessionListerner</code>. The method called having the following signature:</p>
  
  <pre><code>public void sessionDestroyed(HttpSessionEvent se) {
  }
  </code></pre>
  
  <p>I wasn't able to redirect to a specific page. When I want to redirect a user I usually use the <code>NavigationHandler</code> from the <code>FacesContext</code> but in this case there is no <code>FacesContext</code> (<code>FacesContext.getCurrentInstance()</code> returns <code>null</code>).</p>
  
  <p>According to this <a href="https://stackoverflow.com/questions/953572/how-can-i-get-httpservletrequest-when-in-an-httpsessionlistener">post</a>, the HttpListener cannot redirect the user because it is not part of a request.</p>
  
  <p><strong>Question</strong></p>
  
  <p>What is the best way to go to solve this problem? What can I do to make one of the two above-mentioned approaches to work?</p>