java 为一个 servlet 配置 web.xml (Tomcat 5) 以处理所有传入请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/800725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:51:51  来源:igfitidea点击:

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

javatomcatservletsweb.xml

提问by

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.

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

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

我在 web.xml 中配置了以下内容,但它不适用于 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 映射:

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

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

同样的结果。都没有工作......有人有什么建议吗?

回答by groodt

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

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

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

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

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

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

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

Hope this helps.

希望这可以帮助。

回答by jt.

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

<url-pattern>/*</url-pattern>应该管用。您的 web 应用程序需要部署在根上下文中。默认情况下,tomcat 使用名为 ROOT 的 webapp;但是,您可以更改它以在 server.xml 中查找另一个 web 应用程序。

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.

另一种方法是创建一个 ServletFilter 来完成工作并以相同的方式映射它。每种方法(servlet 和 servlet 过滤器)各有利弊。但是,从您的示例来看,您似乎只想将所有内容发送到另一个站点,因此两者都应该可行。

回答by Bhushan Bhangale

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?

模式 /* 将明确地调用您的重定向 servlet。您是否调试了 servlet 以查看它是否收到了对该 url 的请求?你的意思是没有工作?你有没有遇到任何错误或发生了什么?

回答by levik

Did you try

你试过了吗

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

?

?