【IT168 技术文档】以往每添加一个servlet就要在web.xml里添加<servlet><servlet-mapping>,随着项目的进度,servlet数目越来越可观.
web.xml里也密密麻麻的排满了<servlet><servlet-mapping>这些食之无味弃之崩溃的东西,有一种方法可以改善这种境况.
在web.xml里添加如下的配置:
<servlet>
<servlet-name>InvokerServlet</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>-1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InvokerServlet</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
<servlet-name>InvokerServlet</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>-1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InvokerServlet</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
ok,现在web.xml里保留这一个servlet配置就可以满足所有的servlet调用了,比如调用UserManagerService这个servlet,在页面表单里这样写:
<form name="f" method="post" action="/servlet/com.mashiguang.servlet.UserManagerService">
<input/>
<submit/>
</form>
注意表单action "/servlet/"后面是这个servlet class的全名.
<input/>
<submit/>
</form>