views:

3330

answers:

2

Hello All,

I'm running a local Tomcat 6.0 server on my desktop.

I'm trying to redirect any and all requests matching http://localhost:8080/RedirectDirectory/abc/efg/morejunk to a single JSP page.

In my RedirectDirectory project's web.xml I have

<servlet>
<servlet-name>IOPRedirect</servlet-name>
<jsp-file>/RedirectDirectory/filetree.jsp</jsp-file>
</servlet>

<servlet-mapping>
<servlet-name>IOPRedirect</servlet-name>
<url-pattern>/RedirectDirectory/*</url-pattern>
</servlet-mapping>

I would really like it to go to that JSP whether the directory exists or not.

I thought this is how to do it, but I guess not.

Any ideas?

Thanks

A: 

What you did works fine for servlets - haven't tried doing with JSPs.

Edit: After trying more less exactly what you did, I found that it works fine. The exception was when forwarding to a specific jsp which was using a security constraint, which caused an error. The error was due to the fact the redirection got around the user login, and therefore required data was omitted in the request.

A round about way of doing it would be to forward all requests to a servlet & have the servlet forward to your desired jsp.

Your web.xml would then be:

<servlet>
<servlet-name>IOPRedirect</servlet-name>
<servlet-class>IOPRedirect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>IOPRedirect</servlet-name>
<url-pattern>/RedirectDirectory/*</url-pattern>
</servlet-mapping>

And you'd have to create a IOPredirect servlet with the following inside your doGet() method:

String url="/RedirectDirectory/filetree.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);
dborba
+1  A: 

I usually use the UrlRewriteFilter when solving problems like this.

  1. Download and add the urlrewrite.jar to your classpath (WEB-INF/lib)
  2. Add the following to your WEB-INF/web.xml:

    <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  1. Edit WEB-INF/urlrewrite.xml and add the following to it:

    <rule>
        <from>^/RedirectDirectory/(.*)$</from>
        <to>/RedirectDirectory/filetree.jsp</to>
    </rule>

Having UrlRewriteFilter in your project is very handy for solving a lot of common problems like setting cache headers, canonical hostnames, forcing https on certain urls etc.

D. Wroblewski