将特定 servlet 映射为 Tomcat 中的默认 servlet [英] Mapping a specific servlet to be the default servlet in Tomcat

查看:24
本文介绍了将特定 servlet 映射为 Tomcat 中的默认 servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个获取原始请求的 servlet,并决定是处理它们,还是将它们转发到另一个后端服务器.它类似于负载平衡器,其中接收到的请求被转发到(在我的情况下为 2)目的地之一.目的地之一是远程的(在另一台主机上).此外,请求可能会到达根目录(http://mycompany.com/).

I am trying to implement a servlet that gets raw requests, and decide either to process them, or forward them to another backend server. It is similar to a load-balancer, where a received request is forwarded to one of the (in my case 2) destinations. One of the destination is remote (on another host). Furthermore, the requests could come to the root (http://mycompany.com/).

因为我想获得原始请求,所以我实现了自己的 servlet(子类化 HttpServlet),而且效果很好.我的 servlet 看起来像:

Since I want to get raw requests, I implemented my own servlet (subclassing HttpServlet), and that works great. My servlet looks like:

public class MyProxyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        processOrForward(req, resp);
    }
    // also doGet(), doHead(), ...
}

由于我要处理的服务可能会向根发送请求,因此我想将我的 servlet 映射为默认 servlet,从而接收任何没有显式 servlet 映射的请求.假设我的 servlet 的名称是myservlet",并且与另一个 servletfoo"一起运行,我希望所有请求都采用 http://mycompany.com/foo/... 将传送到 foo,其他所有内容(例如/、/bar/...、/myservlet/...)都传送到myservlet".查看之前的帖子(例如,根映射此处此处,或 url 重写 此处),我以为我想通了,但它不起作用.

Since the service I want to process may send requests to the root, I would like to map my servlet to be the default servlet, thereby receiving any request that does not have an explicit servlet mapping. Assume my servlet's name is "myservlet", and is running along side of another servlet "foo", I expect all requests in the form of http://mycompany.com/foo/... to be delivered to foo, and everything else (e.g., /, /bar/..., /myservlet/...) to "myservlet". Looking at earlier posts (eg., root mapping here and here, or url rewriting here), I thought I figured it out, but it does not work.

这是我的 web.xml:

<web-app>
  <servlet>
    <servlet-name>ProxyServlet</servlet-name>
    <servlet-class>com.mycompany.MyProxyServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>ProxyServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

在上面的 web.xml 中,我尝试了 url-pattern

In the above web.xml, for url-pattern I tried

"/" and "/*" and empty (i.e., <url-pattern></url-pattern>), all behave the same -->
    Requests to root (/)goes to tomcat's default servlet
    Requests to /myservlet/... are handled by "myservlet"
    Requests to /fubar/... are always 404

有没有办法将我的 servlet 设为默认值.即,任何没有专门映射到 servlet 的请求都会出现在我的身上(甚至可以接受所有请求,因为我可以将这个 servlet 部署在它自己的容器中).以防万一,我在 Ubuntu 12.10 上使用 Tomcat 7.0.30.

Is there a way of turning my servlet to be the default. I.e., any request that does not map specifically to a servlet comes to mine (it is even acceptable to receive all requests, since I can deploy this servlet in its own container). In case it matters, I am using Tomcat 7.0.30 on Ubuntu 12.10.

推荐答案

这应该对你有用.

来自 Java™ Servlet规范版本 3.1 (JSR 340)

在 Web 应用程序部署描述符中,以下语法用于定义映射:

Chapter 12. Mapping Requests to Servlets

12.2 Specification of Mappings

In the Web application deployment descriptor, the following syntax is used to define mappings:

  • / 字符开头并以 /* 后缀结尾的字符串用于路径映射.

  • A string beginning with a / character and ending with a /* suffix is used for path mapping.

*. 前缀开头的字符串用作扩展映射.

A string beginning with a *. prefix is used as an extension mapping.

空字符串 ("") 是一种特殊的 URL 模式,它精确地映射到应用程序的上下文根,即 http://host:port// 形式的请求.在这种情况下,路径信息是 / 并且 servlet 路径和上下文路径是空字符串("").

The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<contextroot>/. In this case the path info is / and the servlet path and context path is empty string ("").

仅包含 / 字符的字符串表示默认"servlet应用.在这种情况下,servlet 路径是请求 URI 减去上下文路径并且路径信息为空.

A string containing only the / character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.


另外,请阅读这本书中简短示例的精彩解释 Head第一个 Servlets &JSP:通过 Sun 认证 Web 组件开发人员考试(第 2 版)(引用):

三种类型的元素

示例:
/Beer/SelectBeer.do

  • 必须以斜杠 (/) 开头.
  • 可以有扩展名(如 .do),但不是必需的.

示例:
/Beer/*

  • 必须以斜杠 (/) 开头.
  • 总是以斜线/星号 (/*) 结尾.
  • MUST begin with a slash (/).
  • Always ends with a slash/asterisk (/*).

示例:
*.do

  • 必须以星号 (*) 开头(切勿以斜线开头).
  • 在星号之后,它必须有一个点扩展名(.do、.jsp 等).
  • MUST begin with an asterisk (*) (NEVER with a slash).
  • After the asterisk, it MUST have a dot extension (.do, .jsp, etc.).


重要提示:
URL 模式代表逻辑/虚拟结构,即指定的模式(路径)不需要物理存在.

更新

如果你愿意,正如你在评论中所说,

If you want, as you say in your comment,

我希望 host:port 访问我的 servlet,而不是默认的 tomcat servlet

I want host:port to hit my servlet, not the default tomcat servlet

然后在这里查看解决方案:
如何使我的 Web 应用程序成为 Tomcat 默认应用程序

then see the solution here:
How do I make my web application be the Tomcat default application

换句话说,你想要的是一个没有应用上下文的路径,这意味着Tomcat默认应用的应用上下文.

In other words, what you want is a path without application context, which implies the application context of the Tomcat default application.

引自上述链接:

在标准的 Tomcat 安装中,您会注意到在相同的目录(CATALINA_BASE)/webapps/,有一个目录叫ROOT(大写字母很重要,即使在 Windows 下也是如此).那就是当前Tomcat默认应用程序的驻地,即当用户呼叫时立即呼叫http://myhost.company.com[:port].诀窍是把你的应用程序.

In a standard Tomcat installation, you will notice that under the same directory (CATALINA_BASE)/webapps/, there is a directory called ROOT (the capitals are important, even under Windows). That is the residence of the current Tomcat default application, the one that is called right now when a user calls up http://myhost.company.com[:port]. The trick is to put your application in its place.

这篇关于将特定 servlet 映射为 Tomcat 中的默认 servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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