title: Timeout via PrimeFaces p:idleMonitor
body: <p>I am trying to handle the session timeout via <a href="https://www.primefaces.org/showcase/ui/misc/idleMonitor.xhtml" rel="nofollow noreferrer">idlemonitor</a>, a primefaces component. </p>
  
  <p>I am doing this because i need to let the user know that due to inactivity the session has expired. I need to display this message through a dialog, after he closes the dialog he should be redirected to the loginpage. He should not be able to click "back" and browse on the application just as nothing happend; if he clicks "back" he should be redirected to a sessionexpired.xhtml page.</p>
  
  <p>I put the <strong>idleMonitor</strong> in my <strong>loggedintemplate.xhtml</strong>, so it works only if you are logged in, no matter on what page, beacause all my pages, after you logged in, derives from the <strong>loggedintemplate.xhtml</strong>.</p>
  
  <p>This is how the code in my <strong>loggedintemplate.xhtml</strong> looks like:</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;p:idleMonitor timeout="6000" onidle="idleDialog.show()" /&gt;
  
  &lt;p:dialog header="Timeout" resizable="false" closable="false" 
            widgetVar="idleDialog" modal="true"&gt;
      &lt;p:panel styleClass="noborderpanel"&gt;
          &lt;p:panelGrid columns="1" styleClass="adressegrid"&gt;
              &lt;p:outputLabel value="Session has expired due to inactivity" /&gt;
                      &lt;p:commandButton action="#{loginController.timeout()}"
                          value="Ok" /&gt;
          &lt;/p:panelGrid&gt;
      &lt;/p:panel&gt;
  &lt;/p:dialog&gt;
  </code></pre>
  
  <p>So the functionality of this code basically checks if the user was inactive for 6 seconds, if he was inactive, a un-closeable dialog pops up and tells him that the session has expired. </p>
  
  <p>The method <strong>loginController.timeout()</strong> should log the user out, invalidate the session etc. </p>
  
  <p>My problem is that I don't know how to invalidate the session, how to log out the user etc. If I use <code>FacesContext.getCurrentInstance().getExternalContext()
                  .invalidateSession();</code>  it does invalidate the session, but I need more. For example, if the user is inactive for more than 30 min, the default JavaEE timeout time, i get a nullPointerException. </p>
  
  <p>I want to handle the timeout "manually", is there a way to disable the default JavaEE timeout? </p>
  
  <p>What is the best way to handle the timeout manually, not like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;session-config&gt;
      &lt;session-timeout&gt;20&lt;/session-timeout&gt;
  &lt;/session-config&gt;   
  &lt;error-page&gt;
      &lt;exception-type&gt;javax.faces.application.ViewExpiredException&lt;/exception-type&gt;
      &lt;location&gt;/expired.xhtml&lt;/location&gt;
  &lt;/error-page&gt;
  </code></pre>