web.xml常用标签整理(不定期更新)

  1 <?xml version="1.0" encoding="UTF-8"?><!-- 标明使用的XML版本和文档编码,此项必须位于第一行,之前是空行注释都不行 -->

  2 

  3 <!-- 

  4     web.xml学名为配置部署文件,是web应用的入口文件,用于声明系统的各项配置,此文件不是必须的,但也只是最简单的静态项目才没有。

  5     xml文件中大小写敏感,书写次序敏感,自上而下加载,所以配置此文件时要注意标签的顺序和大小写。

  6  -->

  7 

  8 <!-- 

  9     文档声明和系统配置声明,web-app标签内为具体的部署配置项。

 10     xmlns为xml文件的命名空间,xmlns:xsi表示文档遵循的标签规范,xsi:schemaLocation表示xmlschema地址。

 11     以上三项内容可使用IDE生成或者在web容器配置文件内找到。

 12  -->

 13 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 14     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

 15     

 16     <!-- 应用名称,提供GUI工具可能会用来标记这个特定的Web应用的一个名称。 -->

 17     <display-name>Test</display-name>

 18     

 19     <!-- 应用描述,提供网站的描述 -->

 20     <description>此系统用来测试web.xml的各项配置参数</description>

 21     

 22     <!-- 

 23         此元素为空标签,它的存在与否可以指定系统是否可分布式处理。普通系统不添加此标签。

 24         如果web.xml中出现这个元素,则代表站台在开发时已经被设计为能在多个JSP Container 之间分散执行。

 25     -->

 26     <distributable/>

 27     

 28     <!-- 

 29         上下文参数,此项用于声明系统初始化时的参数。子元素均为唯一、必选元素。

 30         在servlet中可以通过getServletContext().getInitParameter("context/param");获取此参数。

 31         在JSP中可以通过${initParam.param_name}获取此参数,例如此系统使用${initParam.testInitParam}。

 32      -->

 33     <context-param>

 34         <param-name>testInitParam</param-name>

 35         <param-value>1</param-value>

 36     </context-param>

 37     

 38     <!-- 

 39         过滤器配置,filter和filter-mapping成对出现,如果没有后者,前者配置可以说是无效。

 40         filter和filter-mapping中的filter-name相同,表示过滤器名;

 41         filter-class表示过滤器的类地址,为Java类路径,不带.java后缀;

 42         async-supported为servlet 3.0新增的属性,标识过滤器是否支持异步处理,默认为false;

 43         init-param表示过滤器初始化时的参数,在过滤器中可通过FilterConfig.getInitParameter(filter-name)获取;

 44         url-pattern表示过滤器处理的请求类型,/*表示过滤所有请求,*.do表示过滤后缀名是do的请求;

 45      -->

 46     <filter>

 47         <filter-name>test</filter-name>

 48         <filter-class>test.TestFilter</filter-class>

 49         <async-supported>true</async-supported>

 50         <init-param>

 51             <param-name>test</param-name>

 52             <param-value>1</param-value>

 53         </init-param>

 54     </filter>

 55     <filter-mapping>

 56         <filter-name>test</filter-name>

 57         <url-pattern>*.do</url-pattern>

 58         <url-pattern>*.html</url-pattern>

 59         <url-pattern>/*</url-pattern>

 60     </filter-mapping>

 61     

 62     <!-- 

 63         该元素用来注册一个监听器类,常与context-param联合使用。。

 64         监听程序中某些属性的变化,具体监听哪些事件根据其实现的接口来定。

 65         HttpSessionListener,HttpSessionAttributeListener,ServletContextListener, ServletContextAttributeListener

 66      -->

 67     <listener>

 68         <listener-class>test.TestListener</listener-class>

 69     </listener>

 70     

 71     <!-- 

 72         Servlet配置,参数含义和Filter类似,此处定义Servlet后在web.xml文件中其他地方引用时可直接引用servlet-name。

 73         init-param表示初始化时的参数,Servlet的参数只能在init()方法内拿到,使用this.getInitParameter("test");  

 74      -->

 75     <servlet>

 76         <servlet-name>testServlet</servlet-name>

 77         <servlet-class>test.TestServlet</servlet-class>

 78         <init-param>

 79             <param-name>test</param-name>

 80             <param-value>1</param-value>

 81         </init-param>

 82         <async-supported>true</async-supported>

 83     </servlet>

 84     <servlet-mapping>

 85         <servlet-name>testServlet</servlet-name>

 86         <url-pattern>/*</url-pattern>

 87     </servlet-mapping>

 88     

 89     <!-- 

 90         session相关配置。

 91         session-timeout表示session超时时间,单位为分钟,为0时表示永不超时,建议设置超时时间,可以一定量的减少session劫持攻击;

 92         tracking-mode表示会话中JSESSIONID存储的位置,可选参数为COOKIE、URL、SSL,推荐使用cookie配合http-only使用,可以通过隐藏会话ID减少session劫持攻击;

 93         cookie-config设置与cookie相关的安全配置,详细参数如下:

 94             name设置cookie的键名

 95             domain设置cookie的有效范围,参数内容为域名,常用与跨域访问cookie,例如.baidu.com表示此cookie在百度域名下可用。

 96             path表示cookie所在的目录,默认为/表示根目录且所有文件都能访问此cookie,/test/表示cookie在/test/下且只有此目录下的文件可以访问。

 97              http-only表示此cookie只能通过HTTP方式进行访问,JS无法读取或修改,此项可以增加网站访问的安全性。

 98              secure为true时表示此cookie只能通过HTTPS连接传递到服务器,而HTTP 连接则不会传递该信息。注意是从浏览器传递到服务器,服务器端的Cookie对象不受此项影响。

 99              max-age以秒为单位表示cookie的生存期,默认为-1表示是临时存在,浏览器关闭时就会消失。

100      -->

101     <session-config>

102         <session-timeout>30</session-timeout>

103         <cookie-config>

104             <name>test</name>

105             <domain>.test.com</domain>

106             <path>fasdfa</path>

107             <http-only>true</http-only>

108             <secure>true</secure>

109             <max-age>-1</max-age>

110         </cookie-config>

111         <tracking-mode>COOKIE</tracking-mode>

112     </session-config>

113     

114     <!-- 

115         指定浏览器对于指定格式的处理方式

116         extension文件的扩展名,例如.doc/.xls/.ppt等

117         mime-type对应文件类型的处理方式

118      -->

119     <mime-mapping>

120         <extension>.docx</extension>

121         <mime-type>application/vnd.openxmlformats-officedocument.wordprocessingml.template</mime-type>

122     </mime-mapping>

123     

124     <!-- 首页面,配置项可以有多个,但会根据上下顺序来加载,如果加载项存在则不会继续,例如index.html存在,则不会加载index.jsp -->

125     <welcome-file-list>

126         <welcome-file>index.html</welcome-file>

127         <welcome-file>index.jsp</welcome-file>

128     </welcome-file-list>

129     

130     <!-- 错误页面配置,有两种方式,一是根据错误代码配置,二是根据错误类型配置 -->

131     <error-page>

132         <error-code>404</error-code>

133         <location>/test/error.jsp</location>

134     </error-page>

135     <error-page>

136         <exception-type>java.lang.NullException</exception-type>

137         <location>/test/error.jsp</location>

138     </error-page>

139     

140     <!-- 

141         JSP页面属性配置

142         taglib可出现多次,表示引入的自定义标签库,taglib-uri表示标签库引用地址,taglib-location表示标签库本地地址

143         jsp-property-group主要用于设置JSP相关属性

144             url-pattern表示匹配哪些文件,例如*.jsp表示所有的jsp文件

145             el-ignored表示是否开启EL表达式支持

146             page-encoding设置用户编码

147             scripting-invalid设置脚本可用性,如果为true则表示JSP页面不支持<%scripting%>语法

148             is-xml为true表示符合url-pattern的文件为JSP页面

149             include-prelude向符合url-pattern的文件开头添加此文件中的内容类似于jsp:include

150             include-coda向符合url-pattern的文件结尾添加此文件中的内容类似于jsp:include

151             deferred-syntax-allowed-as-literal表示JSP是否支持#{}表达式,jsp2.1及以上设置为true,2.0及以下设置为false,否则${}表达式无法解析。

152             trim-directive-whitespaces为true时表示删除模板文件编译时产生的空行,可以在一定程度上增加浏览器解析速度,jsp2.1及其以上支持

153             buffer设置缓冲区的大小,单位KB

154             error-on-undeclared-namespace,默认为false,为true时表示页面编译时如果JSP页面中引用了未声明的标签时会报错(原文: If it is set to true, then an error must be raised during the translation time, when an undeclared tag is used in a JSP page)

155      -->

156     <jsp-config>

157         <taglib>

158             <taglib-uri>http://test.net    </taglib-uri>

159             <taglib-location>/WEB-INF/test.tld</taglib-location>

160         </taglib>

161         <jsp-property-group>

162             <url-pattern></url-pattern>

163             <el-ignored>false</el-ignored>

164             <page-encoding>UTF-8</page-encoding>

165             <scripting-invalid>true</scripting-invalid>

166             <is-xml>true</is-xml>

167             <include-prelude>test.jsp</include-prelude>

168             <include-coda>test.jsp</include-coda>

169             <deferred-syntax-allowed-as-literal>true</deferred-syntax-allowed-as-literal>

170             <trim-directive-whitespaces>true</trim-directive-whitespaces>

171             <buffer>10</buffer>

172             <error-on-undeclared-namespace>true</error-on-undeclared-namespace>

173         </jsp-property-group>

174     </jsp-config>

175     

176     <!-- 

177         待考证标签,在以后的工作中有机会用到时会进行更新

178          security-constraint,login-config,security-role,env-entry,ejb-ref,ejb-local-ref,((service-ref*)), resource-ref*, 

179          resource-env-ref*, message-destination-ref*, persistence-context-ref*, persistence-unit-ref*, post-construct*, 

180          pre-destroy*,data-source*)) | message-destination | locale-encoding-mapping-list)) | absolute-ordering)*

181      -->

182 </web-app>

 

你可能感兴趣的:(web.xml)