views:

3507

answers:

5

Basically I want one servlet to handle all incoming request regardless of the path. I'm on shared hosting environment with access to configure my own web.xml file.

I have the following configured in web.xml, but it doesn't work on Tomcat 5:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=
        "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;  
    <display-name>Redirect</display-name>
    <servlet>
         <display-name>Redirect</display-name>
         <servlet-name>Redirect</servlet-name>
         <servlet-class>com.Redirect</servlet-class>
         <init-param>
            <param-name>host</param-name>
            <param-value>www.myredirectdomain.com</param-value>
        </init-param>
        <init-param>
            <param-name>redirect-type</param-name>
            <param-value>301</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Redirect</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

The above worked for anything starting with a directory in the path such as:

www.mydomain.com/anypath1/anypath2...
www.mydomain.com/anypath1

However, did not work for:

www.mydomain.com/ or
www.mydomain.com

I also tried the following servlet mapping:

<servlet-mapping>
    <servlet-name>Redirect</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

With the same result. Neither worked... Anyone have any suggestions?

A: 

Did you try

<url-pattern>*</url-pattern>

?

levik
No, I will give that a try, but not sure if that is a legal syntax.
A: 

Pattern /* will definetly invoke your Redirect servlet. Did you debug your servlet to see if it received the request for that url? What you mean be did not work? Did you get any error or what happened?

Bhushan
The Redirect servlet only received request that included at least a directory. If I went straight to the domain i.e. www.mydomain.com or www.mydomain.com/ the Redirect servlet was not invoked. The Redirect servlet works as intended. The error I get is 403 Forbidden. However, if I put a index.jsp file at the root I get no error.
Use <welcome-file-list>, if you don't want to use a index page.
Adeel Ansari
I see you edited your question now. Don't you have any context name for your webapplication where you configured web.xml or the web.xml is tomcat's root context? I doubt that it will work. You can define index.jsp in welcome-file-list and in the jsp redirect the request to your servlet.
Bhushan
A: 

<url-pattern>/*</url-pattern> should work. Your webapp needs to be deployed at the root context. By default, tomcat uses the webapp named ROOT; however, you could change it to look for another webapp in your server.xml.

Another approach would be to create a ServletFilter to do the work and map it the same way. There a pros and cons to each approach (servlet and servlet filter). However, from your example, it looks like you just want to send everything to another site, so either should work.

jt
+2  A: 

Tomcat 5 implements the Servlet 2.4 Specification. It can be downloaded here: JCP Servlet 2.4 Spec

On pg. 86 - SRV.11.2 it describes how to specify Servlet mappings. If I understand what you are trying to do correctly, you are trying to intercept every request(no matter what the path) to your server with a single Servlet. For that to work, your webapp needs to be mounted at default context ("ROOT") in the case of Tomcat and your Servlet needs to mapped to the default servlet in your web.xml. Your mapping in your web.xml is correct.

<servlet-mapping>
    <servlet-name>Redirect</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

I think the problem you are having is with the ROOT context. What does accessing www.mydomain.com/ and www.mydomain.com display? You dont mention if your shared hosting environment gives you full access to your own Tomcat config, but if you can access and modify your $TOMCAT5_HOME/conf directory, there are a few ways to make this work for you.

Probably the cleanest way is to add the following:

< Context path="" debug="0" docBase="your-app">

to $TOMCAT5_HOME/conf/server.xml. This assumes your applications called "your-app.war".

Hope this helps.

cuberoot
Does it matter if Context path="" or path="/"? I've seen it defined as "/" in the past.
Taylor Leese
A: 

hiii......... i have a similar situation to handle... the thing is i have servlets performing certain functions..... but i need all the links to those servlets mapped to a particular servlet to check the requesting ip.....if i found it to be legal under my condition then i need to map that request to a servlet of interest to the client....... can u help me on this.....