Response.sendRedirect not working in Servlet Filter

The following filter is in my prj2 . From this prj2 I check if there is a session that I want to redirect to the prj1 login page that has url /prj1/sessionexpiry . In the following scenario, it is not redirected to the login page and does not throw any exceptions.

 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse hsr = (HttpServletResponse) res; HttpServletRequest hreq = (HttpServletRequest) req; HttpSession session = hreq.getSession(false); if (session == null) { hsr.sendRedirect("/prj1/sessionexpiry"); return; } else { chain.doFilter(req, res); } } 

Any help would be appreciated

+5
source share
1 answer

Did you invalidate the session in the exit server or somewhere else? Otherwise, the session will never be zero.

 request.getSession().invalidate(); 

Use this to cancel a session, and then when you check if the session is valid or not, it will get a null value.

0
source

All Articles