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;
}
}
<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............
<global-results>
<result name="sessionexpired" >Session_Timeout.jsp</result>
</global-results>
--------------------------------------------------------------------------------
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>
This comment has been removed by the author.
ReplyDeletethis is not working the session cannot timeout may be because of some jQuery is countinously requesting the action class .please help me
ReplyDeleteI had also written the same thing once for my <a href="http://www.javaexperience.com/force-page-to-login-after-struts2-session-timeout/'>struts2 application</a> and in addition what you have shown here, I had added javascript alert when the session is going to timeout.
ReplyDelete