You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Hertenstein Alain <al...@gva.eri.ch> on 2004/03/24 14:09:02 UTC

URL redirect problem, even with 'RequestDispatcher' servlet !

Hi all,
 
This question has already been asked before, but the solutions found in
the archives don't seem to work properly.
 
I have a web site running on IIS and Tomcat 4.1.24 under the root
context, let's say http://www.mysite.com
When people access the URL, it automatically redirects to the context's
default page "index.html", making the http://www.mysite.com/index.html
URL to appear in the browser. I'd like to avoid this and only have
http://www.mysite.com in the location bar instead. BUT I only want this
behaviour for the default page only, NOT for other pages (f.ex.
http://www.mysite.com/info/home.htm should remain unchanged in the
location bar).
 
In the Tomcat list archives, some people mentioned about writing a
servlet which would forward the request to the appropriate page. Here's
what I've done :
 
1) I've added added this in the web.xml :
 
<servlet>
    <servlet-name>indexredirect</servlet-name>
    <servlet-class>test.servlets.IndexRedirect</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>indexredirect</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
 
2) I've created the IndexRedirect servlet and added it in the
WEB-INF/classes/etc... directory, whose doGet() method looks like this :
 
  public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);

    String requestUri = request.getRequestURI(),
        contextPath = request.getContextPath(),
        requestedPage = "";
 
    requestedPage = requestUri.replaceFirst(contextPath, ""); //
request.getServletPath() seems to do it too, but doesn't always work ?
    System.out.println("requestedPage : "+requestedPage);

    if (requestedPage.equals("/")) {
        System.out.println("DEFAULT Forwarding request to index.html
!");
        RequestDispatcher reqDispatcher =
request.getRequestDispatcher(response.encodeURL("index.html"));
        reqDispatcher.forward(request, response);
    }
// Commented out because redirecting to the requested page starts an
infinite loop !!!
/* else {
        System.out.println("Forward request to "+requestedPage+"...");
        RequestDispatcher reqDispatcher =
request.getRequestDispatcher(response.encodeURL(requestedPage));
        reqDispatcher.forward(request, response);
    }*/
}

After testing the http://www.mysite.com/ URL, the page redirects to
index.html the first time, which is fine. But then, I don't know why but
the servlet is called again, detects that the requested URL is
http://www.mysite.com/index.html now, and would go to the "else"
statement (here commented out), and thus forward the request to
index.html again !! Infinite loop...
 
My questions are :
- I don't understand why the servlet is called whenever the request URI
is, although the <url-pattern> tag in web.xml is defined as "/" (so the
servlet should only be called when the context's root page is requested,
i.e. http://www.mysite.com/ ?).
- How can I have this thing done ? Is there something I've forgotten
here ?
 
Thanks a lot for your help, this problem is really puzzling me...
 
Note : I'm not using Apache, so mod_rewrite is not an option for me.
 
Best regards
Alain Hertenstein
 


**********************************************************************
 This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
**********************************************************************


Re: URL redirect problem, even with 'RequestDispatcher' servlet !

Posted by Tim Funk <fu...@joedog.org>.
/ is also known as the default servlet. So your servlet will also need to 
server all static content too.


-Tim

Hertenstein Alain wrote:

> Hi all,
>  
> This question has already been asked before, but the solutions found in
> the archives don't seem to work properly.
>  
> I have a web site running on IIS and Tomcat 4.1.24 under the root
> context, let's say http://www.mysite.com
> When people access the URL, it automatically redirects to the context's
> default page "index.html", making the http://www.mysite.com/index.html
> URL to appear in the browser. I'd like to avoid this and only have
> http://www.mysite.com in the location bar instead. BUT I only want this
> behaviour for the default page only, NOT for other pages (f.ex.
> http://www.mysite.com/info/home.htm should remain unchanged in the
> location bar).
>  
> In the Tomcat list archives, some people mentioned about writing a
> servlet which would forward the request to the appropriate page. Here's
> what I've done :
>  
> 1) I've added added this in the web.xml :
>  
> <servlet>
>     <servlet-name>indexredirect</servlet-name>
>     <servlet-class>test.servlets.IndexRedirect</servlet-class>
> </servlet>
> <servlet-mapping>
>     <servlet-name>indexredirect</servlet-name>
>     <url-pattern>/</url-pattern>
> </servlet-mapping>
>  
> 2) I've created the IndexRedirect servlet and added it in the
> WEB-INF/classes/etc... directory, whose doGet() method looks like this :
>  
>   public void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>     response.setContentType(CONTENT_TYPE);
> 
>     String requestUri = request.getRequestURI(),
>         contextPath = request.getContextPath(),
>         requestedPage = "";
>  
>     requestedPage = requestUri.replaceFirst(contextPath, ""); //
> request.getServletPath() seems to do it too, but doesn't always work ?
>     System.out.println("requestedPage : "+requestedPage);
> 
>     if (requestedPage.equals("/")) {
>         System.out.println("DEFAULT Forwarding request to index.html
> !");
>         RequestDispatcher reqDispatcher =
> request.getRequestDispatcher(response.encodeURL("index.html"));
>         reqDispatcher.forward(request, response);
>     }
> // Commented out because redirecting to the requested page starts an
> infinite loop !!!
> /* else {
>         System.out.println("Forward request to "+requestedPage+"...");
>         RequestDispatcher reqDispatcher =
> request.getRequestDispatcher(response.encodeURL(requestedPage));
>         reqDispatcher.forward(request, response);
>     }*/
> }
> 
> After testing the http://www.mysite.com/ URL, the page redirects to
> index.html the first time, which is fine. But then, I don't know why but
> the servlet is called again, detects that the requested URL is
> http://www.mysite.com/index.html now, and would go to the "else"
> statement (here commented out), and thus forward the request to
> index.html again !! Infinite loop...
>  
> My questions are :
> - I don't understand why the servlet is called whenever the request URI
> is, although the <url-pattern> tag in web.xml is defined as "/" (so the
> servlet should only be called when the context's root page is requested,
> i.e. http://www.mysite.com/ ?).
> - How can I have this thing done ? Is there something I've forgotten
> here ?
>  
> Thanks a lot for your help, this problem is really puzzling me...
>  
> Note : I'm not using Apache, so mod_rewrite is not an option for me.
>  
> Best regards
> Alain Hertenstein


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org