We usually check if the session has ended at the filter level and match it DispatcherServletso that all incoming requests that will be processed using spring will be filtered out first and thus will not allow any a spring interaction if the session has already expired. If the session has expired, send a redirect to a page where the user will be informed that their session has already expired.
Filter Code Example
public class MyFilter implements Filter{
...
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (isSessionExpired((HttpServletRequest) theRequest)) {
response.sendRedirect(((HttpServletRequest) theRequest).getContextPath() + "/expired.jsp");
response.flushBuffer();
}else{
theChain.doFilter(theRequest, theResp);
}
}
...
}
Map to DispatcherServletin web.xml
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.mycompany.ourproject.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<servlet-name>springdispatcher</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>springdispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
source
share