Windows的IIS,是用UI界面进行站点的配置;Linux下面的几乎所有系统,都是使用配置文件来进行配置,Java容器(JBoss/Tomcat/Jetty/WebSphere/WebLogic等等)也不例外,它们使用一个部署在WEB-INFO目录下面的web.xml来作为站点配置文件。
本文参考互联网文章,学习并记录web.xml的加载顺序及配置详解。
<context-param />
用来设定web站点的环境参数
它包含两个子元素:<param-name></param-name> 用来指定参数的名称;<param-value></param-value> 用来设定参数值
在此设定的参数,可以在servlet中用 getServletContext().getInitParameter("my_param") 来取得
例子:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>privilege.root</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext*.xml
classpath*:/cas-authority.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
用来设定Listener接口
它的主要子元素为 <listener-class></listener-class> ,用来定义Listener的类名称
例子:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
用来声明filter的相关设定
<filter-name></filter-name> 指定filter的名字
<filter-class></filter-class> 用来定义filter的类的名称
<init-param></init-param> 用来定义参数,它有两个子元素: <param-name></param-name> 用来指定参数的名称, <param-value></param-value> 用来设定参数值
与一起使用的是
<filter-mapping></filter-mapping> 用来定义filter所对应的URL,包含两个子元素:
<filter-name></filter-name> 指定filter的名称
<url-pattern></url-pattern> 指定filter所对应的URL
例子:
<!-- 解决中文乱码问题 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
用来声明一个servlet的数据,主要有以下子元素:
<servlet-name></servlet-name> 指定servlet的名称
<servlet-class></servlet-class> 指定servlet的类名称
<jsp-file></jsp-file> 指定web站台中的某个JSP网页的完整路径
<init-param></init-param> 用来定义参数
与一起使用的是
<servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素:
<servlet-name></servlet-name> 指定servlet的名称
<url-pattern></url-pattern> 指定servlet所对应的URL
例子:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 用dubbo提供hessian服务 -->
<servlet>
<servlet-name>dubbo</servlet-name>
<servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dubbo</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
基本节点
1、<description/> 是对站点的描述
例子:传道、授业、解惑
2、<display-name/> 定义站点的名称
例子:<display-name>我的站点</display-name>
3、
icon元素包含small-icon和large-icon两个子元素,用来指定web站点中小图标和大图标的路径。
<small-icon>/路径/smallicon.gif</small-icon>
small-icon元素应指向web站台中某个小图标的路径,大小为16 X 16 pixel,但是图象文件必须为GIF或JPEG格式,扩展名必须为:.gif或.jpg。
<large-icon>/路径/largeicon-jpg</large-icon>
large-icon元素应指向web站台中某个大图表路径,大小为32 X 32 pixel,但是图象文件必须为GIF或JPEG的格式,扩展名必须为: gif或jpg。
例子:
/images/small.gif /images/large.gir4、 <distributable/> 是指定该站点是否可分布式处理
5、 <session-config/> 用来定义web站台中的session参数
包含一个子元素:
<session-timeout></session-timeout> 用来定义这个web站台所有session的有效期限,单位为 分钟
6、 <mime-mapping /> 定义某一个扩展名和某一个MIME Type做对应,它包含两个子元素:
扩展名的名称<mime-type></mime-type> MIME格式
例子:
<mime-mapping>
<extension>csv</extension>
<mime-type>application/octet-stream</mime-type>
</mime-mapping>
7、 <error-page>
通过错误码来配置error-page
<error-page>
<error-code>404</error-code>
<location>/message.jsp</location>
</error-page>
通过异常类来配置error-page
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
8、 <welcome-file-list/>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
9、 <resource-ref></resource-ref> 定义利用JNDI取得站台可利用的资源
有五个子元素:
资源说明<rec-ref-name></rec-ref-name> 资源名称
<res-type></res-type> 资源种类
<res-auth></res-auth> 资源经由Application或Container来许可
<res-sharing-scope></res-sharing-scope> 资源是否可以共享,有Shareable和Unshareable两个值,默认为Shareable
比如,配置数据库连接池就可在此配置
<resource-ref>
JNDI JDBC DataSource of shop<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>