views:

24

answers:

1

Hi,

I am using a class which implements Filter for my jsp stuff. It looks like this:

public class MyFilter implements Filter 
{
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
        throws IOException, ServletException 
    {
        request.getRequestDispatcher("mypage.jsp").forward(request, response); 
    }
}

So the target, "mypage.jsp", is just sitting in my top-level directory. The filter works fine if I'm entering urls like:

http://www.mysite.com/foo
http://www.mysite.com/boo

but if I enter a trailing slash, I'll get a 404:

http://www.mysite.com/foo/
http://www.mysite.com/boo/

HTTP ERROR: 404
/foo/mypage.jsp

RequestURI=/foo/mypage.jsp

it seems if I enter the trailing slash, then the filter thinks I want it to look for mypage.jsp in subfolder foo or boo, but I really always want it to just find it at:

http://www.mysite.com/mypage.jsp

how can I do that?

Thank you

A: 

Make the request dispatcher path absolute - use "/mypage.jsp" rather than "mypage.jsp". The resource is then resolved from the root of the webapp context.

mdma
Thanks that worked! I found that my css files and other relative resources could not be resolved, I had to update their paths in mypage.jsp to also use the forward slash before the path, like "csspath=/mypage.css" instead of "csspath=mypage.css", is that ok?Thanks!
I'm pleased it works, but I'm not sure this is a good idea. Why do you want to allow the user to enter any arbitrary URL that maps to the root?
mdma
Well I want to support something like twitter, where www.mysite/username gets handled by one jsp page that just generates a page for them. I can't make a real folder per user, so thought this type of 'url rewriting' would work? Thanks
Ok, but rather than hard-coding in a slash - use a method call to apply the slash, that way, you can quickly relocate the resources if necessary.
mdma