Struts 2 Session TimeOut

I am new to location. I am using Struts2. Please, anyone can tell me how to automatically redirect the jsp page after the session has timed out.

+6
source share
2 answers

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"; // session is empty/expired return invocation.invoke(); } 

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) { // Put your logic here to check session } } 

For automatic redirection you need to continue checking the session with some kind of Ajax call

+19
source

For automatic redirection, you need to make an Ajax request and see if the session still has a known value. If the session ends, it will be invalid by the container, and something like the currentUser object currentUser no longer be in the session.

If this object no longer exists, your Ajax call will return either a flag, a redirect URL, etc., and the Ajax success method will check that, whatever you specify, they need to be redirected and the window location set to constant or value The redirect URL returned by the Ajax call.

+4
source

Source: https://habr.com/ru/post/906338/


All Articles