Tomcat中的invoker

FROM:http://www.darkmi.com/blog/archives/878

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

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

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

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

(1)

<servlet> <servlet-name>invoker</servlet-name> <servlet-class> org.apache.catalina.servlets.InvokerServlet </servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>

(2)

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

来个例子:
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文件中添加如下配置信息:

<servlet> <description>just for test</description> <display-name>test servlet</display-name> <servlet-name>HelloWorld</servlet-name> <servlet-class>mypack.HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/servlet/HelloWorld</url-pattern> </servlet-mapping>

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

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

你可能感兴趣的:(tomcat,servlet,Servlets,url,deprecated,initialization)