How java struts2 handle session expired using Interceptor

Published on Author bloggerfiveLeave a comment

Create Custom Interceptor
=====================

import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.log4j.Logger;

public class SessionInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = -2501154646552984007L;

static final Logger sessionLogger = Logger.getLogger(SessionInterceptor.class);

public String intercept(ActionInvocation actionInvocation) throws Exception {

ActionContext context = actionInvocation.getInvocationContext();
Map<String, Object> sessionMap = context.getSession();

if(sessionMap.containsKey(“SESSION_SERVICE”))
{

if(sessionMap == null
|| sessionMap.isEmpty()
|| sessionMap.get(“SESSION_SERVICE”) == null) {
sessionLogger.info(“User Session Expired”);
return “SESSION_EXPIRED”;
}

}

String actionResult = actionInvocation.invoke();

return actionResult;
}
}

Struts.xml
========

<interceptor name=”SessionInterceptor”/>
<interceptor-stack name=”SessionCheckStack”>
<interceptor-ref name=”SessionInterceptor” />
<interceptor-ref name=”defaultStack” />
</interceptor-stack>
</interceptors>

<default-interceptor-ref name=”SessionCheckStack”/>

Action Class
=========
session.put(“SESSION_SERVICE” ,Boolean.valueOf(true));
session.put(“user”,  getUser());
session.put(“username”,  getName());
session.put(“userPassword”,  getPassword());
session.put(“userId”,  getUserId());
session.put(“userRole”,  getUserRole());

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *