views:

47

answers:

3

Hi, 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); 
  } 
} 
A: 

You can check if the requested path is in your "excluded list" with request.getServletPath().

If you want a new Filter separated from SessionFilter, you could either set a special flag as request attribute (such as "loginPage") which will be checked by other filters (if you want a new Filter separated from SessionFilter) or you can simply not invoke the chain.doFilter().

If you're modifying SessionFilter, just don't redispatch to "login.jsp"

Colin Hebert
Thanks for your reply. I am not sure how to not invoke chain.doFilter().
JustToHelp
@JustToHelp, simply don't use this statement ;)
Colin Hebert
@JustToHelp: you are already doing it at the code you have pasted (the last code line, inside the else block).
Tomas Narros
I am trying to redirect users to my login page if there is an invalid session. if(!session.isNew()){ filterChain.doFilter(request, response); } else{ rd = request.getRequestDispatcher("login.jsp"); rd.forward(request, response); }The problem is that if I use requestdispatcher, there will be an infinite loop when the users access login.jsp.
JustToHelp
@JustToHelp, so if `request.getServletPath()` is your login page, don't call the `filterChain.doFilter`, this way other filters won't be called.
Colin Hebert
I am modifying my SessionFilter but I am still having the error. String path = req.getServletPath();if(!session.isNew()){ filterChain.doFilter(request, response); } else if(path.indexOf("login.jsp")!=-1){ //do nothing } else{ rd = request.getRequestDispatcher("login.jsp"); rd.forward(request, response); }
JustToHelp
If session isn't new (because your didn't go through the login page) the chain will be done, you have to check the servlet path first.
Colin Hebert
ah, I see what you mean. But even with checking at the if statement it still cant run. String path = req.getServletPath(); int a = -1; a= path.indexOf("login.jsp"); HttpSession session = req.getSession(); if(!session.isNew() ||a==-1){ filterChain.doFilter(request, response); } else{ rd = request.getRequestDispatcher("login.jsp"); rd.forward(request, response); }
JustToHelp
@JustToHelp, I was more thinking about this code : http://ideone.com/H1xjk
Colin Hebert
adding return does not work, it will give me an empty page
JustToHelp
I added a print message inside the if statement. The SessionFilter works, all the other page will print the same message. But how to redirect to login.jsp without causing an infinite loop?
JustToHelp
ok I got it to work by manually inputing the url pattern instead of a generic /*
JustToHelp
A: 

You can add another if-clause (or another condition in the existing one) that checks whether the current requestURI is not in a list of preconfigured files. This list can be passed with an <init-param> to the filter

Bozho
+1  A: 

You don't need a new filter. You could check it at your own filter.

if(request.getServletPath().equals("login.jsp") || !session.isNew()) { // or "/login.jsp", not sure about this
   filterChain.doFilter(request, response); 
} else { // session is new and it's not login.jsp
     rd = request.getRequestDispatcher("login.jsp"); 
     rd.forward(request, response);    
}

But I don't like this kind of approach. It seems like rewriting the JAAS API for servlets. And it implies hardcoding jsp paths, which could be not a good idea in order of maintenance and portability.

Tomas Narros