web.xml文件简单说明

参考文章:http://www.cnblogs.com/morlin/p/4301063.html
说明:各参数的加载顺序与在web.xml文件中的顺序无关。其加载顺序为:ServletContext>context-param>listener>filter>servlert。

配置说明

开头,所有元素都要放在<web-app></web-app>中。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>DGCZYW.BMS</display-name>

</web-app>

1、<display-name></display-name>用于定义站台的名称
<display-name>DGCZYW.BMS</display-name>
2、<discription></discription>是对站台的描述
3、<context-param></context-param>用于设定web站台的环境参数,其包含两个子元素:
<param-name> </param-name>用于指定参数的名称;
<param-value> </param-value>用来设定参数值。
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath*:log4j.properties</param-value>
  </context-param>
注:此参数可以在servlet中用getServletContext().getInitParameter("log4jConfigLocation")来获得;
4、<filter></filter>是用来声明filter的相关设定,其包含以下子元素:
<filter-name> </filter-name>用于指定filter的名字;
< filter-class> </filter-class>用来定义filter类的名称;
<init-param> </init-param>用来定义参数,它有两个子元素:
<param-name> </param-name> 用来指定参数的名称;
<param-value> </param-value> 用来设定参数值。
	<filter>
		<filter-name>DruidWebStatFilter</filter-name>
		<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
		<init-param>
			<param-name>exclusions</param-name>
			<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
		</init-param>
	</filter>
5、<filter-mapping></filter-mapping>与<filter></filter>同时使用,用来定义filter所对应的URL,其包含两个元素:
<filter-name></filter-name> 指定filter的名字;
<url-patttern></url-pattern> 指定filter所对应的URL。
	<filter-mapping>
		<filter-name>DruidWebStatFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
6、<servlet></servlet> 用来声明一个servlet的数据,主要有以下子元素:
<servlet-name></servlet-name> 用于指定servlet的名称;
<servlet-calss></servlet-class> 用于指定servlet的类名称;
<init-param></init-param> 用来定义参数,同上;
<jsp-file></jsp-file> 指定web站台中某个jsp网页的完整路径;
<multipart-config> </multipart-config> 上传文件配置:
<max-file-size> </max-file-size> 单个文件最大大小;
<max-request-size> </max-request-size> 所有文件最大大小。
	<!-- Spring MVC配置 -->
	<servlet> 
	  <servlet-name>SpringMVC</servlet-name> 
	  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
	  <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml--> 
	  <init-param> 
	    <param-name>contextConfigLocation</param-name> 
	    <param-value>classpath:SpringMVC-servlet.xml</param-value>
	  </init-param> 	  
	  <load-on-startup>1</load-on-startup> 
	  <multipart-config>
	  	<max-file-size>3145728</max-file-size><!-- 单个文件最大大小:3M -->
	  	<max-request-size>10485760</max-request-size><!-- 所有文件最大大小:10M -->
	  </multipart-config>
	</servlet> 

注:更一种写法

<param-value>/WEB-INF/classes/application-mvc.xml</param-value>
7、<servlet-mapping></servlet-mapping>,与<servlet> </servlet>同时使用,用来定义servlet所对应的URL,包含两个子元素:
<servlet-name></servlet-name>指定servlet的名称;
<url-pattern></url-pattern>指定servlet所对应的URL。      
	<servlet-mapping> 
	  <servlet-name>SpringMVC</servlet-name> 
	  <url-pattern>/</url-pattern> 
	</servlet-mapping>
8、<welcome-file-list></welcom-file-list>用来定义首页的列单,包含一个子元素:
<welcome-file></welcome-file>指定首页的文件名称;
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
9、<error-page></error-page>用来处理错误代码或异常的页面,有三个子元素:   
< error-code></error-code> 指定错误代码;
<exception-type></exception-type> 指定一个JAVA异常类 ;
<location></location> 指定在web站台内的相关资源路径。
<error-page>
     <error-code>404</error-code>
     <location>/error404.jsp</location>
 </error-page>
 <error-page>
     <exception-type>java.lang.Exception</exception-type>
     <location>/exception.jsp</location>
 </error-page>
10、<listener></listener> 声明监听相关设定:
<listener-class></listen-class> 指定监听类,该类需要继承ServletContextListener类包含初始化方法contextInitialized(ServletC ontextEvent event)和销毁方法 contextDestoryed(ServletContextEvent event)
 <!-- Spring配置 -->
 <listener> 
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 </listener>  

附加:

1、spring-mvc web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>BMS</display-name>
  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath*:log4j.properties</param-value>
  </context-param>  
 <!-- Spring配置 -->
 <listener> 
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 </listener>    

 <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
 <context-param> 
   <param-name>contextConfigLocation</param-name> 
   <param-value>classpath:applicationContext.xml</param-value> 
 </context-param>

 <!-- Spring MVC配置 -->
 <servlet> 
   <servlet-name>SpringMVC</servlet-name> 
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
   <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml--> 
   <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:SpringMVC-servlet.xml</param-value>
   </init-param>    
   <load-on-startup>1</load-on-startup> 
   <multipart-config>
    <max-file-size>3145728</max-file-size><!-- 单个文件最大大小:3M -->
    <max-request-size>10485760</max-request-size><!-- 所有文件最大大小:10M -->
   </multipart-config>
 </servlet> 

 <servlet-mapping> 
   <servlet-name>SpringMVC</servlet-name> 
   <url-pattern>/</url-pattern> 
 </servlet-mapping>

 <filter>
  <filter-name>DruidWebStatFilter</filter-name>
  <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
  <init-param>
   <param-name>exclusions</param-name>
   <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>DruidWebStatFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <servlet>
  <servlet-name>DruidStatView</servlet-name>
  <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>DruidStatView</servlet-name>
  <url-pattern>/druid/*</url-pattern>
 </servlet-mapping> 
</web-app>
2、Struts2 web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 id="WebApp_ID" version="3.1">
  <display-name>BMS</display-name>
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
     org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值