Monday, April 21, 2014

How to handle session time out Programatically in ADF ? How to configure Session Time out ?

This blog explains about how to redirect to specific URL when session time out occurs. By default, default session out time is 45 minutes if nothing specified

This blog also explains about how to configure session time out in weblogic.xml as well as in any ADF application


Below is how we can configure session time out for any ADF Application, This can be done through web.xml

<session-config>
  <session-timeout>10</session-timeout>
</session-config>

You can edit the File weblogic.xml: Edit the session-param TimeoutSecs in the file weblogic.xml. In weblogic.xml, the session timeout is set in seconds.

 <session-descriptor>
       <session-param>
           <param-name>TimeoutSecs</param-name>
          <param-value>3600</param-value>
      </session-param>
 </session-descriptor>

You can also configure sesion time out using weblogic console as well,
Login to Weblogic console -> Deployments -> Click on the application -> Follow below step


Note that the timeout value set in web.xml takes precedence over weblogic.xml. If you don't set any values in web.xml, weblogic.xml takes over. A good approach to handle session timeout is setting this just on web.xml itself since web.xml takes precedence over application server’s deployment descriptors.




- Write below SessionExpiryFilter class, In this class I am verifying the old and new session Id values, if both are different means, session is expired and redirecting the respective URL.

       

  package com.fm.recruitment.view.filter;
  
  import java.io.IOException;
  
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
   
  public class SessionExpiryFilter implements Filter {
      private FilterConfig _filterConfig = null;
   
      public SessionExpiryFilter() {
          super();
      }
   
      public void init(FilterConfig filterConfig) throws ServletException {
          _filterConfig = filterConfig;
      }
   
      public void doFilter(ServletRequest servletRequest,
                           ServletResponse servletResponse,
                           FilterChain filterChain) throws IOException,
                                                           ServletException {
          String requestedSession =
              ((HttpServletRequest)servletRequest).getRequestedSessionId();
          String currentWebSession =
              ((HttpServletRequest)servletRequest).getSession().getId();
          String requestURI =
              ((HttpServletRequest)servletRequest).getRequestURI();
          boolean sessionOk =
              currentWebSession.equalsIgnoreCase(requestedSession);
          System.out.println("currentWebSession == requestedSession? : " + sessionOk);
          if (!sessionOk && requestedSession != null) {
              requestURI="http://www.google.com";
              ((HttpServletResponse)servletResponse).sendRedirect(requestURI);
              System.out.println("redirecting to : " + requestURI);
          } else {
              filterChain.doFilter(servletRequest, servletResponse);
              System.out.println("session is OK");
          }
      }
   
      public void destroy() {
          _filterConfig = null;
      }
  }

       
 

- Below is how we configure Filter through web.xml

 <filter>
 <filter-name>SessionExpiryFilter</filter-name>
   <filter-class>com.fm.recruitment.view.filter.SessionExpiryFilter</filter-class>
 </filter>
<filter-mapping>
  <filter-name>SessionExpiryFilter</filter-name>
   <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

More about Deployment descriptor or web.xml
http://docs.oracle.com/cd/E15523_01/web.1111/e13712/web_xml.htm#i1023849

No comments:

Post a Comment