tomcat invoker

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

<!-- 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进行配置的美观简练,所以仅限开发调试时用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值