一、web.xml文件作用
在WEB-INF文件夹下的web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。当你的web工程没用到这些时,你可以不用web.xml文件来配置你的web工程,也就是说在web工程中可以没有web.xml文件。
二、元素详解
<?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">
每个xml文件都有定义它书写规则的Schema文件,也就是说javaEE的定义web.xml所对应的xml Schema文件中定义了多少种标签元素,web.xml中就可以出现它所定义的标签元素,也就具备哪些特定的功能。所有部署描述符文件的顶层(根)元素为web-app。请注意,XML元素不像HTML,他们是大小写敏感的。因此,web-App和WEB-APP都是不合法的,web-app必须用小写。以上web-app是web.xml的根节点标签名称;xmlns是web.xml文件用到的命名空间;xmlns:xsi是指web.xml遵守xml规范xsi:schemaLocation是指具体用到的schema资源 。
<?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>***</display-name>
<!--声明WEB应用的描述信息-->
<description>***</description>
<!--icon元素包含small-icon和large-icon两个子元素.用来指定web站台中小图标和大图标的路径.-->
<icon>
<!--small-icon元素应指向web站台中某个小图标的路径,大小为16 X 16 pixel,但是图象文件必须为GIF或JPEG格式,扩展名必须为:.gif或.jpg.-->
<small-icon>/images/small.gif</small-icon>
<!--large-icon元素应指向web站台中某个大图表路径,大小为32 X 32 pixel,但是图象文件必须为GIF或JPEG的格式,扩展名必须为; gif或jpg.-->
<large-icon>/images/large.gir</large-icon>
</icon>
<!--指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!--元素用来设定web站台的环境参数(context),它包含两个子元素:param-name和param-value.-->
<!-- 此所设定的参数,在JSP网页中可以使用下列方法来取得:
${initParam.param_name}
若在Servlet可以使用下列方法来获得:
String param_name=getServletContext().getInitParamter("param_name");-->
<context-param>
<!--设定Context名称-->
<param-name>log4jConfigLocation</param-name>
<!--设定Context名称的值-->
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<!--API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。Listener元素指出事件监听程序类。-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!--在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。-->
<!--注意:元素出现地次序不是随意的。特别是,需要把所有servlet元素放在所有 servlet-mapping元素之前。-->
<servlet>
<!--为Servlet命名-->
<servlet-name>springMVC</servlet-name>
<!--这表示这个org.springframework.web.servlet.DispatcherServlet的servlet已经得到了注册名springMVC-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 利 用init-param元素向servlet提供初始化参数,init-param元素具有param-name和param-value子元素。-->
<init-param>
<!--方法中调用getServletConfig(). getInitParameter("contextConfigLocation")获得"spring-mvc.xml"-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-mvc.xml</param-value>
</init-param>
<!--当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--<servlet-mapping>元素在Servlet和URL样式之间定义一个映射-->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!--以一个单独的/指示这个Web应用程序是默认的Servlet-->
<!--如果对某个请求没有找到匹配的Servlet,那么将使用Web应用程序的默认Servlet来处理。-->
<!--<url-pattern>元素指定对应于Servlet的URL路径,该路径是相对于Web应用程序上下文根的路径。-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- filter类似于servlet-->
<filter>
<filter-name>encodingFilter</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>
<!-- error-page元素包含三个子元素error-code,exception-type和location.将错误代码(Error Code)或异常(Exception)的种类对应到web站台资源路径.-->
<error-page>
<error-code>403</error-code>
<location>/error/403.jsp</location>
</error-page>
<!--会话超时配置(单位为分钟)-->
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>
web.xml文件元素的加载顺序与在文中的先后顺序无关,真正的加载顺序为:context-param -> listener -> filter -> servlet
参考