简体   繁体   中英

Configure web.xml (Tomcat 5) for one servlet to handle all incoming requests?

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">  
    <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?

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.

<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 are 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.

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?

Did you try

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

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM