---
title: "Tomcat的invoker使用总结"
author: "hou_jian_yong"
date: "2011-02-28T10:53:52Z"
tags: ["Apache", "Servlet", "Tomcat", "Web", "XML"]
categories: ["企业架构"]
slug: "tomcat-invoker"
draft: false
lastmod: "2011-02-28T10:53:52Z"
id: 933191
url: "https://espace.iteye.com/blog/933191"
---

## Tomcat的invoker使用总结

invoker为方便servlet的开发与调试开启了一道大门，免去了开发一个servlet就要在web.xml文件中添加相应配置信息的麻烦。以下是位于$TOMCAT\_HOME/conf/web.xml文件中对于invoker的说明：

&lt;!-- The "invoker" servlet, which executes anonymous servlet classes      --&gt;  
&lt;!-- that have not been defined in a web.xml file.  Traditionally, this   --&gt;  
&lt;!-- servlet is mapped to the URL pattern "/servlet/\*", but you can map   --&gt;  
&lt;!-- it to other patterns as well.  The extra path info portion of such a --&gt;  
&lt;!-- request must be the fully qualified class name of a Java class that  --&gt;  
&lt;!-- implements Servlet (or extends HttpServlet), or the servlet name     --&gt;  
&lt;!-- of an existing servlet definition.     This servlet supports the     --&gt;  
&lt;!-- following initialization parameters (default values are in square    --&gt;  
&lt;!-- brackets):                                                           --&gt;  
&lt;!--                                                                      --&gt;  
&lt;!--   debug               Debugging detail level for messages logged     --&gt;  
&lt;!--                       by this servlet.  \[0]                          --&gt;

简单翻译如下：invoker可以运行没有在web.xml文件中配置的servlet。默认情况下，通过在URL中添加“/servlet/\*”来进行映射，当然也可以通过其他URL模式进行映射。被访问的Java类必须使用完全限定名，同时还需要实现Servlet（或者HttpServlet）接口，或则是实现继承自Servlet接口的其他子接口。

将Tomcat安装目录下conf目录中的web.xml文件打开，找到如下两端代码，去掉其注释即可打开invoker功能：  
**(1)**

&lt;servlet&gt;  
&lt;servlet-name&gt;invoker&lt;/servlet-name&gt;  
&lt;servlet-class&gt;  
org.apache.catalina.servlets.InvokerServlet  
&lt;/servlet-class&gt;  
&lt;init-param&gt;  
&lt;param-name&gt;debug&lt;/param-name&gt;  
&lt;param-value&gt;0&lt;/param-value&gt;  
&lt;/init-param&gt;  
&lt;load-on-startup&gt;2&lt;/load-on-startup&gt;  
&lt;/servlet&gt;

**(2)**

&lt;servlet-mapping&gt;  
&lt;servlet-name&gt;invoker&lt;/servlet-name&gt;  
&lt;url-pattern&gt;/servlet/\*&lt;/url-pattern&gt;  
&lt;/servlet-mapping&gt;

来个例子：  
web应用myapp的目录结构如下：

myapp  
|——otherfiles  
|——WEB-INF  
     |——classes  
          |——mypack  
               |——HelloWorld.class

打开invoker功能之后，访问HelloWorld.class的URL为：http://localhost:8080/myapp/servlet/mypack.HelloWorld

如果没有打开invoker功能，那么还需要在myapp应用的web.xml文件中添加如下配置信息：

&lt;servlet&gt;  
&lt;description&gt;just for test&lt;/description&gt;  
&lt;display-name&gt;test servlet&lt;/display-name&gt;  
&lt;servlet-name&gt;HelloWorld&lt;/servlet-name&gt;  
&lt;servlet-class&gt;mypack.HelloWorld&lt;/servlet-class&gt;  
&lt;/servlet&gt;

&lt;servlet-mapping&gt;  
&lt;servlet-name&gt;HelloWorld&lt;/servlet-name&gt;  
&lt;url-pattern&gt;/servlet/HelloWorld&lt;/url-pattern&gt;  
&lt;/servlet-mapping&gt;

然后通过如下URL访问HelloWorld：http://localhost:8080/myapp/servlet/HelloWorld

缺点：会暴露一些开发信息比如类的完全限定名，而且如果完全限定名很长的话，URL也不如在web.xml进行配置的美观简练，所以仅限开发调试时用。