1 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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	
<!-- 	context-param元素声明应用范围内的初始化参数。-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:/spring/spring.xml,
			classpath:/spring/spring-mybatis.xml
		</param-value>
	</context-param>

	
	<!-- <listener> <listener-class>com.ane.aneweb.util.SysContextListener </listener-class> 
		</listener> -->
	<listener>
		<description>spring监听器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 防止spring内存溢出监听器 -->
	<!-- <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 
		</listener> -->
	<!-- 拦截未登录  -->
<!-- 	<filter> 
		<filter-name>sessionFilter</filter-name> 
		<filter-class>com.ane.online.util.SessionFilter</filter-class> 
	</filter> 
	<filter-mapping> 
		<filter-name>sessionFilter</filter-name> 
		<url-pattern>/user/*</url-pattern> 
		<url-pattern>/order/*</url-pattern> 
		<url-pattern>/excel/*</url-pattern> 
		<url-pattern>/payee/*</url-pattern> 
	</filter-mapping> -->
	<!--字符集过滤器 -->
	
	<filter>
		<description>字符集过滤器</description>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<description>字符集编码</description>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

    <servlet>
      <servlet-name>ImageServlet</servlet-name>
      <servlet-class>com.pda.common.servlet.ImageServlet</servlet-class>
      <init-param>
        <param-name>width</param-name>
        <param-value>65</param-value>
      </init-param>
      <init-param>
        <param-name>height</param-name>
        <param-value>26</param-value>
      </init-param>
      <init-param>
        <param-name>codeCount</param-name>
        <param-value>4</param-value>
      </init-param>
    </servlet>
    <servlet-mapping>
      <servlet-name>ImageServlet</servlet-name>
      <url-pattern>/ImageServlet</url-pattern>
    </servlet-mapping>

	<!-- spring mvc servlet -->
	<servlet>
		<description>spring mvc servlet</description>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>spring mvc</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:/spring/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <servlet-class>  
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>  
        <load-on-startup>2</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/webservice/*</url-pattern>  
    </servlet-mapping>
    
     <servlet>  
	    <servlet-name>log4j-init</servlet-name>  
	    <servlet-class>com.pda.thread.Log4jInit</servlet-class>  
	    <init-param>  
	        <param-name>log4j-init-file</param-name>  
	        <param-value>/WEB-INF/classes/log4j.properties</param-value>  
	    </init-param>  
	    <load-on-startup>3</load-on-startup>  
    </servlet>
    
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
</web-app>

  

2 Web.xml常用元素  3 <web-app>  4 <display-name></display-name>定义了WEB应用的名字  5 <description></description> 声明WEB应用的描述信息  6  7 <context-param></context-param> context-param元素声明应用范围内的初始化参数。  8 <filter></filter> 过滤器元素将一个名字与一个实现<a href="http://www.cnblogs.com/bukudekong/archive/2011/12/26/2302183.html">javax.servlet.Filter</a>接口的类相关联。  9 <filter-mapping></filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。  10 <listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。  11  Listener元素指出事件监听程序类。  12 <servlet></servlet> 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。  13 <servlet-mapping></servlet-mapping> 服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。   14 <span style="color: #008000;"> 但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。</span>在更改缺省URL时,使用servlet-mapping元素。  15  16 <session-config></session-config> 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。  17 可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。  18  19 <mime-mapping></mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。  20 <welcome-file-list></welcome-file-list> 指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。  21 <error-page></error-page> 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。  22 <taglib></taglib> 对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,  23  而不用编辑使用这些文件的JSP页面。  24 <resource-env-ref></resource-env-ref>声明与资源相关的一个管理对象。  25 <resource-ref></resource-ref> 声明一个资源工厂使用的外部资源。  26 <security-constraint></security-constraint> 制定应该保护的URL。它与login-config元素联合使用  27 <login-config></login-config> 指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用。  28 <security-role></security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素  29 的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。  30 <env-entry></env-entry>声明Web应用的环境项。  31 <ejb-ref></ejb-ref>声明一个EJB的主目录的引用。  32 < ejb-local-ref></ ejb-local-ref>声明一个EJB的本地主目录的应用。  33 </web-app>  34     35  36 web.xml元素介绍  37 每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定.  38  39 复制代码  40 1.<?xml version="1.0" encoding="ISO-8859-1"?>  41 部署描述符文件就像所有XML文件一样,必须以一个XML头开始。这个头声明可以使用的XML版本并给出文件的字符编码。  42 2.<web-app xmlns="http://java.sun.com/xml/ns/j2ee"  43 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  44 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  45 version="2.4">  46 <web-app>  47 这是一般在写XML时所做的声明,定义了XML的版本,编码格式,还有重要的指明schema的来源,为http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd.  48 3.<description>站台描述</discription>对站台做出描述.  49 4.<display-name>站台名称</display-name>定义站台的名称.  50 <icon>icon元素包含small-icon和large-icon两个子元素.用来指定web站台中小图标和大图标的路径.  51 <small-icon>/路径/smallicon.gif</small-icon>  52 small-icon元素应指向web站台中某个小图标的路径,大小为16 X 16 pixel,但是图象文件必须为GIF或JPEG格式,扩展名必须为:.gif或.jpg.  53 <large-icon>/路径/largeicon-jpg</large-icon>  54 large-icon元素应指向web站台中某个大图表路径,大小为32 X 32 pixel,但是图象文件必须为GIF或JPEG的格式,扩展名必须为; gif或jpg.  55 范例:  56 <display-name>Develop Example</display-name>  57 <description>JSP 2.0 Tech Book's Examples</description>  58 <icon>  59 <small-icon>/images/small.gif</small-icon>  60 <large-icon>/images/large.gir</large-icon>  61 </icon>  62 5.<distributable>  63 distributable 元素为空标签,它的存在与否可以指定站台是否可分布式处理.如果web.xml中出现这个元素,则代表站台在开发时已经  64 被设计为能在多个JSP Container 之间分散执行.  65 范例:  66 6.<context-param>  67 context-param 元素用来设定web站台的环境参数(context),它包含两个子元素:  68 param-name和param-value.  69 <param-name>参数名称</param-name>  70 设定Context名称  71 <param-value>值</param-value>  72 设定Context名称的值  73 </context-param>  74 范例:  75 <context-param>  76 <param-name>param_name</param-name>  77 <param-value>param_value</param-value>  78 </context-param>  79 此所设定的参数,在JSP网页中可以使用下列方法来取得:  80 ${initParam.param_name}  81 若在Servlet可以使用下列方法来获得:  82 String param_name=getServletContext().getInitParamter("param_name");  83 7.<filter>  84 filter元素用来声明filter的相关设定.filter元素除了下面介绍的的子元素之外,还包括<servlet>介绍过的<icon>,<display-name>  85 ,<description>,<init-param>,其用途一样.  86 <filter-name>Filter的名称</filter-name>  87 定义Filter的名称.  88 <filter-class>Filter的类名称</filter-class>  89 定义Filter的类名称.例如:com.foo.hello  90 </filter>  91 范例:  92 <filter>  93 <filter-name>setCharacterEncoding</filter-name>  94 <filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>  95 <init-param>  96 <param-name>encoding</param-name>  97 <param-value>GB2312</param-value>  98 </init-param>  99 </filter> 100 8.<filter-mapping> 101 <filter-mapping> 102 filter-mapping 元素的两个主要子元素filter-name和url-pattern.用来定义Filter所对应的URL. 103 <filter-name>Filter的名称</filter-name> 104 定义Filter的名称. 105 <url-pattern>URL</url-pattern> 106 Filter所对应的RUL.例如:<url-pattern>/Filter/Hello</url-pattern> 107 9.<servlet-name>Servlet的名称<servlet-name> 108 定义servlet的名称. 109 <dispatcher>REQUEST|INCLUDE|FORWARD|ERROR</disaptcher> 110 设定Filter对应的请求方式,有RQUEST,INCLUDE,FORWAR,ERROR四种,默认为REQUEST. 111 </filter-mapping> 112 范例: 113 <filter-mapping> 114 <filter-name>GZIPEncoding</filter-name> 115 <url-pattern>/*</url-pattern> 116 </filter-mapping> 117 10.<listener> 118 listener元素用来定义Listener接口,它的主要子元素为<listener-class> 119 <listen-class>Listener的类名称</listener-class> 120 定义Listener的类名称.例如: com.foo.hello 121 <listener> 122 范例: 123 <listener> 124 <listener-class>coreservlet.javaworld.CH11.ContenxtListener</listener-class> 125 </listener> 126 11.<servlet-mapping> 127 servlet-mapping元素包含两个子元素servlet-name和url-pattern.用来定义servlet所对应URL. 128 <servlet-name>Servlet的名称</servlet-name> 129 定义Servlet的名称. 130 <url-pattern>Servlet URL</url-pattern> 131 定义Servlet所对应的RUL.例如:<url-pattern>/Servlet/Hello</url-pattern> 132 </servlet-mapping> 133 范例: 134 <servlet-mapping> 135 <servlet-name>LoginChecker</servlet-name> 136 <url-pattern>/LoginChecker</url-pattern> 137 </servlet-mapping> 138 12.<session-cofing> 139 session-config包含一个子元素session-timeout.定义web站台中的session参数. 140 <session-timeout>分钟</session-timeout> 141 定义这个web站台所有session的有效期限.单位为分钟. 142 </session-config> 143 范例: 144 <session-config> 145 <session-timeout>20</session-timeout> 146 </session-config> 147 13.<mime-mapping> 148 mime-mapping包含两个子元素extension和mime-type.定义某一个扩展名和某一MIME Type做对映. 149 <extension>扩展名名称</extension> 150 扩展名称 151 <mime-type>MIME格式</mime-type> 152 MIME格式. 153 </mime-mapping> 154 范例: 155 <mime-mapping> 156 <extension>doc</extension> 157 <mime-type>application/vnd.ms-word</mime-type> 158 </mime-mapping> 159 <mime-mapping> 160 <extension>xls</extension> 161 <mime-type>application/vnd.ms-excel</mime-type> 162 </mime-mapping> 163 <mime-mapping> 164 <extension>ppt</extesnion> 165 <mime-type>application/vnd.ms-powerpoint</mime-type> 166 </mime-mapping> 167 14.<welcome-file-list> 168 welcome-file-list包含一个子元素welcome-file.用来定义首页列单. 169 <welcome-file>用来指定首页文件名称</welcome-flie> 170 welcome-file用来指定首页文件名称.我们可以用<welcome-file>指定几个首页,而服务器会依照设定的顺序来找首页. 171 范例: 172 <welcome-file-list> 173 <welcome-file>index.jsp</welcome-file> 174 <welcome-file>index.htm</welcome-file> 175 </welcome-file-list> 176 15.<error-page> 177 error-page元素包含三个子元素error-code,exception-type和location.将错误代码(Error Code)或异常(Exception)的种类对应 178 到web站台资源路径. 179 <error-code>错误代码</error-code> 180 HTTP Error code,例如: 404 181 <exception-type>Exception</exception-type> 182 一个完整名称的Java异常类型 183 <location>/路径</location> 184 在web站台内的相关资源路径 185 </error-page> 186 范例: 187 <error-page> 188 <error-code>404</error-code> 189 <location>/error404.jsp</location> 190 </error-page> 191 <error-page> 192 <exception-type>java.lang.Exception</exception-type> 193 <location>/except.jsp</location> 194 </error-page> 195 16.<jsp-config> 196 jsp-config元素主要用来设定JSP的相关配置,<jsp:config>包括<taglib>和<jsp-property-group>两个子元素.其中<taglib>元素在JSP 1.2时就已经存在了;而<jsp-property-group>是JSP 2.0新增的元素. 197 <taglib> 198 taglib元素包含两个子元素taglib-uri和taglib-location.用来设定JSP网页用到的Tag Library路径. 199 <taglib-uri>URI</taglib-uri> 200 taglib-uri定义TLD文件的URI,JSP网页的taglib指令可以经由这个URI存取到TLD文件. 201 <taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction> 202 TLD文件对应Web站台的存放位置. 203 </taglib> 204 <jsp-property-group> 205 jsp-property-group元素包含8个元素,分别为: 206 <description>Description</descrition> 207 此设定的说明 208 <display-name>Name</display-name> 209 此设定的名称 210 <url-pattern>URL</url-pattern> 211 设定值所影响的范围,如:/CH2 或者/*.jsp 212 <el-ignored>true|false</el-ignored> 213 若为true,表示不支持EL语法. 214 <scripting-invalid>true|false</scripting-invalid> 215 若为true表示不支持<%scription%>语法. 216 17.<page-encoding>encoding</page-encoding> 217 设定JSP网页的编码 218 18.<include-prelude>.jspf</include-prelude> 219 设置JSP网页的抬头,扩展名为.jspf 220 JSP segments(以前版本称为JSP fragments)即jspf文件是不完整的JSPs,是用来被其他的JSP来包含的。 221 19.<include-coda>.jspf</include-coda> 222 设置JSP网页的结尾,扩展名为.jspf 223 </jsp-property-group> 224 </jsp-config> 225 范例: 226 <jsp-config> 227 <taglib> 228 <taglib-uri>Taglib</taglib-uri> 229 <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location> 230 </taglib> 231 <jsp-property-group> 232 <description> 233 Special property group for JSP Configuration JSP example. 234 </description> 235 <display-name>JSPConfiguration</display-name> 236 <uri-pattern>/*</uri-pattern> 237 <el-ignored>true</el-ignored> 238 <page-encoding>GB2312</page-encoding> 239 <scripting-inivalid>true</scripting-inivalid> 240 ............ 241 </jsp-property-group> 242 </jsp-config> 243 20.<resource-ref> 244 resource-ref元素包括五个子元素description,res-ref-name,res-type,res-auth,res-sharing-scope.利用JNDI取得站台可 245 利用资源. 246 <description>说明</description> 247 资源说明 248 <rec-ref-name>资源名称</rec-ref-name> 249 资源名称 250 <res-type>资源种类</res-type> 251 资源种类 252 <res-auth>Application|Container</res-auth> 253 资源由Application或Container来许可 254 <res-sharing-scope>Shareable|Unshareable</res-sharing-scope> 255 资源是否可以共享.默认值为 Shareable 256 范例: 257 <resource-ref> 258 <description>JNDI JDBC DataSource of JSPBook</description> 259 <res-ref-name>jdbc/sample_db</res-ref-name> 260 <res-type>javax.sql.DataSoruce</res-type> 261 <res-auth>Container</res-auth> 262 </resource-ref>
抱怨没有用,只能靠自己
原文地址:https://www.cnblogs.com/mybatis/p/5902537.html