I am trying to implement a filter for all my files excluding login.jsp. I understand that filter mapping cannot exclude certain files. What I need to do is to create another filter to map just the login.jsp. How do I create another file that with url pattern /login.jsp and without SessionFilter being processed after it?
Here is part of my code for session filter for all files.
public class SessionFilter implements Filter{
RequestDispatcher rd = null;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException{
HttpServletRequest request = (HttpServletRequest)request;
HttpSession session = request.getSession();
// New Session so forward to login.jsp
if (session.isNew()){
rd = request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
}
// Not a new session so continue to the requested resource
else{
filterChain.doFilter(request, response);
}
}
You can check if the requested path is in your “excluded list” with
request.getServletPath().If you want a new
Filterseparated fromSessionFilter, you could either set a special flag as request attribute (such as “loginPage”) which will be checked by other filters (if you want a newFilterseparated fromSessionFilter) or you can simply not invoke thechain.doFilter().If you’re modifying
SessionFilter, just don’t redispatch to “login.jsp”