views:

938

answers:

2

Hello,

I'm having a problem with my authentication filter. When the filter redirects to the login page, no images gets displayed in the login JSP. However, if I go to the login page manually after I'm logged in, the images are displayed.

I don't understand why this is happening! I appreciate any help. :-)

AuthFilter:

if (authorized == null && path.indexOf("Auth") == -1 && path.indexOf("Login") == -1 ) {
        httpResponse.sendRedirect("Login");  
        return;  
} else {  
        chain.doFilter(request, response);  
}

Login servlet:

// Just using a servlet in case I want more data sent to the jsp
Dispatcher.dispatch("views/login.jsp", request, response);

login.jsp:

<img src="images/logo.png" />

The jsp is otherwise "normal", all required HTML tags are present. The "images" folder is in the default "web" folder of the project, alongside all the other jsp's and javascripts.

Thanks in advance for any help. :)
- Stian

+2  A: 

Could it be that your filter is also applied to image requests and redirects the request for logo.png to login.jsp?

If so, you could adjust the filter-mapping in web.xml.

Thilo
I think you're on to something here. When I viewed the source of login.jsp in firefox, and then clicked on the link to the image I got a 404: The requested resource (/PJ600/images/Login) is not available.What do you mean by adjusting the filter-mapping?
Stian
+3  A: 

It's because of the relative paths.

  • your Login is in the root of the context
  • your images probably are /views/images/
  • when you forward, the browser knows only the requested URL.

So when you forward, the images are sought at /images (because they are relative to the current address) instead of /views/images/

How to resolve it. Two options:

  • don't forward from your servlet; redirect instead
  • don't redirect to the servlet from the filter; redirect to the login page directly

Update: Make sure the images are NOT affected by the filter. two options:

  • they should not be matched by the filter pattern
  • redirection should not happen for .png, .jpeg, .css, etc. in the filter. check this with request.getRequestURI()
Bozho
I tried redirecting to login.jsp from the filter, but that didn't work.. When I viewed the source in Firefox and clicked on the image, it said that the page wasn't redirecting properly. So it's probably the filter that's causing this problem.
Stian
ah, then you have to exclude the images from the filter - see my update
Bozho
Thanks for the help, now I know where the problem is. :)
Stian