Well, you need to create a way to check if the session has ended or not, since the browser was unable to find out if the session has expired or not. You need to follow these steps.
Define the session time in your web.xml, for example.
<session-config> <session-timeout> 30 </session-timeout> </session-config>
One easy way to get Struts2 to create an Interceptor and check the expiration of the session, and if the session has expired, you can redirect the user back to the specified jsp page. Here is an overview of the trial interceptor
interceptor
public class SessionInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { Map<String,Object> session = invocation.getInvocationContext().getSession(); if(session.isEmpty()) return "session";
Finally, you need to tell Stuts2 that you want to use this interceptor by declaring it in the struts.xml file
struts.xml
<interceptor name="session" class="myapp.interceptor.SessionInterceptor" /> <interceptor-stack name="sessionExpirayStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="session"/> </interceptor-stack>
Now you need to use this stack declaration in your actions.
Action configuration
<action name="myAction" class="myClass"> <interceptor-ref name="sessionExpirayStack" /> <result name="success">success.jsp</result> <result name="session">sessionexpired.jsp</result> </action>
Alternatively, you can declare a global result for *session* so that use is redirected to the same page around the world.
To go beyond Struts2, you have the option to create a servlet filter and you can place the session verification code inside the filter. All you need to do is enter the javax.servlet.Filter interface
Session Verification Filter
public final CheckSession impliments Filter{ private FilterConfig filterConfig = null; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void destroy() { this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
For automatic redirection you need to continue checking the session with some kind of Ajax call