为一个servlet配置web.xml(Tomcat 5)以处理所有传入请求? [英] Configure web.xml (Tomcat 5) for one servlet to handle all incoming requests?

查看:109
本文介绍了为一个servlet配置web.xml(Tomcat 5)以处理所有传入请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要一个servlet来处理所有传入的请求,而不管路径如何。我在共享托管环境中可以访问配置我自己的web.xml文件。

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.

我在web.xml中配置了以下内容,但是它无法正常工作Tomcat 5:

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

但是,不起作用:

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

我也试过了以下servlet映射:

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实现了Servlet 2.4规范。它可以在这里下载:
JCP Servlet 2.4 Spec

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

on pg。 86 - SRV.11.2它描述了如何指定Servlet映射。如果我理解您正在尝试正确执行的操作,那么您将尝试使用单个Servlet拦截到服务器的每个请求(无论路径是什么)。为了实现这一点,在Tomcat的情况下,您的webapp需要挂载在默认上下文(ROOT)中,并且您的Servlet需要映射到web.xml中的默认servlet。您在web.xml中的映射是正确的。

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>

我认为你遇到的问题是ROOT环境。访问www.mydomain.com/和www.mydomain.com会显示什么?你没有提到你的共享托管环境是否可以完全访问你自己的Tomcat配置,但是如果你可以访问和修改你的$ TOMCAT5_HOME / conf目录,有几种方法可以让你工作。

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

到$ TOMCAT5_HOME / conf / server.xml。假设您的应用程序名为your-app.war。

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

希望这会有所帮助。

这篇关于为一个servlet配置web.xml(Tomcat 5)以处理所有传入请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆