Session timeout interceptor in struts 2

Interceptor continue.....
If you want to use an interceptor which will check whether the session has been expired or not, if it is expired then it will forward to global view( like any jsp that you want to display after session timeout).

You need to do the following coding...
1) Make an interceptor file SessionCheckInterceptor .java

----------------------------------------------------

package com.example.web.interceptor;


import java.util.Map;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.vicwalks.web.util.LogManager;
import com.vicwalks.web.util.VicWalkConstants;


public class SessionCheckInterceptor implements Interceptor {

private static final long serialVersionUID = 1L;


public void destroy() {
LogManager.info(this," SessionCheckInterceptor destroy() is called...");
}


public void init() {
LogManager.info(this," SessionCheckInterceptor init() is called...");
}


public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext context = actionInvocation.getInvocationContext();
Map<String, Object> sessionMap = context.getSession();
LogManager.info(this," retrived session..."+ sessionMap);
if(sessionMap == null || sessionMap.isEmpty() || sessionMap.get(VicWalkConstants.USER_ID)==null) {
 LogManager.info(this," session expired...");
 return "sessionexpired";
}
String actionResult = actionInvocation.invoke();
return actionResult;
}


}
-------------------------------------------------------------------------------
2) Now do the interceptor entry in struts.xml file...

<interceptors>
       <interceptor name="SessionCheckInterceptor"  
                 class="com.vicwalks.web.interceptor.SessionCheckInterceptor"/>
      <interceptor-stack name="testSessionValidationStack">
                <interceptor-ref name="SessionCheckInterceptor" />
               <interceptor-ref name="defaultStack" />
      </interceptor-stack> 
         
</interceptors>



 <default-interceptor-ref name="testSessionValidationStack"/> 
  <global-results>
           <result name="sessionexpired" >Session_Timeout.jsp</result>
  </global-results> 

--------------------------------------------------------------------------------

Remember if you will use <default-interceptor> in struts.xml file, then it will work for all actions.

Now if you want to apply this interceptor only for some particular actions then you can do it separately inside the action....
---------------------change in struts.xml............

<interceptors>
       <interceptor name="SessionCheckInterceptor"  
                 class="com.vicwalks.web.interceptor.SessionCheckInterceptor"/>
      <interceptor-stack name="testSessionValidationStack">
                <interceptor-ref name="SessionCheckInterceptor" />
               <interceptor-ref name="defaultStack" />
      </interceptor-stack> 
         
</interceptors>




  <global-results>
           <result name="sessionexpired" >Session_Timeout.jsp</result>
  </global-results> 



<action name="testLogin" class="com.example.web.action.TestLoginAction">
   <interceptor-ref name="testSessionValidationStack"/>
<result name="success">LoginSuccess.jsp</result>
<result name="input">login.jsp</result>
<result name="error">error.jsp</result>
</action>


--------------------------------------------------------------------------------
3) Don't forget to see the session configuration in web.xml file under WEB-INF/classes directory

<session-config>
      <session-timeout>30</session-timeout> 
  </session-config>
where 30 minutes represents maximum session time. So if a user is inactive till 30 minutes, session will be timeout and then if you are going to execute some action, interceptor will check the and forward you to the jsp whatever you have suggested in struts.xml under  <global-results>

Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Upload file on Google cloud storage using Java servlet on google app engine

Android: where to run keytool command in android