I'm trying to find a method to redirect my request from filter to login page but I don't know how to redirect from servlet. I searched but what I find is sendRedirect() method. I can't find this method on my response obect in filter. What's the cause? How can I solve this?
解决方案
In Filter the response is of ServletResponse rather than HttpServletResponse.Hence do the cast to HttpServletResponse.
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("/login.jsp");
If using a context path:
httpResponse.sendRedirect(req.getContextPath() + "/login.jsp");
Also dont forget to call return; at the end.