首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

关于Tomcat中支配Web应用,web.xml中的welcome-file-list配置

2012-08-24 
关于Tomcat中部署Web应用,web.xml中的welcome-file-list配置基于Servlet的Web应用,部署描述文件如下:?xml

关于Tomcat中部署Web应用,web.xml中的welcome-file-list配置
基于Servlet的Web应用,部署描述文件如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>Transaction Monitor</display-name>  <listener>    <listener-class>    org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/application-context.xml</param-value>  </context-param>  <filter>    <filter-name>encoding-filter</filter-name>    <filter-class>            org.springframework.web.filter.CharacterEncodingFilter        </filter-class>    <init-param>      <param-name>encoding</param-name>      <param-value>UTF-8</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>encoding-filter</filter-name>    <url-pattern>*.do</url-pattern>  </filter-mapping>  <servlet>    <servlet-name>myservlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value></param-value>    </init-param>  </servlet>  <servlet-mapping>    <servlet-name>myservlet</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.do</welcome-file>  </welcome-file-list></web-app>


在这里,使用的是SpringMVC(但是也许使用Struts的话),问题可能也类似(没试过)
虽然在welcome-file-list中配置了index.do,不过实际上总是返回404错误,提示找不到。但如果直接在地址中输入完整的URL(含index.do),则总是可以访问。

参考了网友一篇文章:
http://yjshengshe.iteye.com/blog/641934

在应用根目录下添加一个空文件(或者任何内容的文件),只要文件名为 index.do 就可以解决如上问题。

虽然这招看上去有点猥琐,我还是想了想原因。在请求地址没有index.do的情况下(例如:http://localhost:8080/appname/ ,这个时候应答这个请求的是tomcat自身的web请求处理器,并且这个请求不符合 *.do的模式,所以也不会转发给springmvc处理。tomcat自带的web服务器只是简单的检查一下有没有index.do这个资源,没有的就直接返回了404.

为了避免tomcat直接返回404,添加一个假的index.do资源,tomcat于是就傻傻通过welcome-file-list找到并且认可index.do这个资源,并且匹配这个资源;然后发现其符合*.do的URL模式,于是就转交给springmvc了。

如果我的推测合理,那么这种结果给我的感觉不是很舒坦。

热点排行