Currently I use the following filter to redirect user to index page after session is expired.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpSession httpSession = httpRequest.getSession(false);
if (httpSession != null && !httpSession.isNew()) {
chain.doFilter(request, response);
} else {
httpResponse.sendRedirect(request.getServletContext().getContextPath() + PathManager.getPagePath("index"));
}
}
But can the session be expired when executing code in doPost/doGet methods? So on entering the filter the session is ok, but the session expires inside the servlet's doPost/doGet method.
If such a scenario can happen, what is the solution to redirect the user to the login page?