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

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

问题描述

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



由于我想获得原始请求,我实现了自己的servlet(子类化 HttpServlet ),这很有用。我的servlet看起来像:

 公共类MyProxyServlet扩展HttpServlet {
@Override
protected void doPost(HttpServletRequest req,HttpServletResponse resp)
抛出ServletException,IOException {
processOrForward(req,resp);
}
//也是doGet(),doHead(),...
}

由于我想要处理的服务可以向root发送请求,我想将我的servlet映射为默认的servlet,从而接收任何没有显式servlet映射的请求。假设我的servlet名称是myservlet,并且正在另一个servletfoo的旁边运行,我希望所有请求都以 http://mycompany.com/foo/ ...将被传递到foo,以及其他所有内容(例如/,/ bar / ...,/ myservlet / ...)到myservlet 。查看之前的帖子(例如,根映射此处此处,或网址重写这里),我以为我弄明白了,但它不起作用。



这是我的 web.xml

 < web应用程序> 
< 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我试过

 /和/ *并为空(即< url-pattern>< / url-pattern>) ,一切都表现相同 - > 
对root(/)的请求转到tomcat的默认servlet
对/ myservlet / ...的请求由myservlet处理
对/ fubar / ...的请求总是404

有没有办法让我的servlet成为默认值。即,任何未专门映射到servlet的请求都会出现(我甚至可以接收所有请求,因为我可以在自己的容器中部署这个servlet)。如果它很重要,我在Ubuntu 12.10上使用Tomcat 7.0.30。

解决方案

这应该对你有用。



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


第12章。将请求映射到Servlet



12.2映射规范



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




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


  • *。前缀开头的字符串用作扩展名映射


  • 空字符串()是一个特殊的URL模式,它完全映射到
    应用程序的上下文根
    ,即 http:// host:port /< contextroot> / 形式的请求。
    在这种情况下,路径信息是 / ,并且servlet路径和上下文路径是
    空字符串()


  • 仅包含 / 字符的字符串表示defaultservlet 申请的>。在这种情况下,servlet路径是请求URI减去上下文路径
    ,路径信息为null。


  • 所有其他字符串仅用于完全匹配




作为补充,请阅读本书 Head First Servlets& JSP:通过Sun认证Web组件开发人员考试(第2版)(引用):


三种类型< url-pattern> 元素



1)完全匹配



示例:

< url-pattern> /Beer/SelectBeer.do< / url-pattern>




  • 必须以斜杠开头( / )。

  • 可以有一个扩展名(如.do),但不是必需的。



2)DIRECTORY match



示例:

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




  • 必须以斜杠开头( / ) 。

  • 总是以斜杠/星号结尾( / * )。



3)EXTENSION匹配



示例:

< url-pattern> *。执行< / url-pattern>




  • 必须以星号( * )开头(绝对不带斜线)。

  • 之后星号,它必须有一个点扩展名(.do,.jsp等)。




  • 重要提示:

    网址模式代表逻辑/虚拟结构,即模式(路径)指定不需要实际存在






    更新



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


    我想要host:port to点击我的servlet,而不是默认的tomcat servlet


    然后在这里看到解决方案:

    如何使我的Web应用程序成为Tomcat默认应用程序



    换句话说,你想要的是没有ap的路径 plication context ,这意味着Tomcat默认应用程序的应用程序上下文。



    从以上链接引用:


    在标准的Tomcat安装中,您会注意到在同一个
    目录(CATALINA_BASE)/ webapps /下,有一个名为 ROOT $的目录b $ b(即使在Windows下,大写字母也很重要)。 这是当前Tomcat默认应用程序的
    住所,当用户调用
    http:// myhost时,现在调用
    。 company.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/).

    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(), ...
    }
    

    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.

    Here is my 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>
    

    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
    

    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.

    解决方案

    This should be useful to you.

    From the Java™ Servlet Specification Version 3.1 (JSR 340)

    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.

    • 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 ("").

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

    • All other strings are used for exact matches only.


    As an addition, read this nice explanation with short examples from the book Head First Servlets & JSP: Passing the Sun Certified Web Component Developer Exam (2nd edition) (quote):

    The THREE types of <url-pattern> elements

    1) EXACT match

    Example:
    <url-pattern>/Beer/SelectBeer.do</url-pattern>

    • MUST begin with a slash (/).
    • Can have an extension (like .do), but it’s not required.

    2) DIRECTORY match

    Example:
    <url-pattern>/Beer/*</url-pattern>

    • MUST begin with a slash (/).
    • Always ends with a slash/asterisk (/*).

    3) EXTENSION match

    Example:
    <url-pattern>*.do</url-pattern>

    • MUST begin with an asterisk (*) (NEVER with a slash).
    • After the asterisk, it MUST have a dot extension (.do, .jsp, etc.).


    IMPORTANT NOTE:
    The URL patterns represent logical / virtual structure, i.e. the patterns (paths) specified does not need to exist physically.


    UPDATE

    If you want, as you say in your comment,

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

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

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

    Quote from the above link:

    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天全站免登陆