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>

 

转载于:https://www.cnblogs.com/aotian/p/4633824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: web.xml文件中有多种标签用于配置和初始化Web应用程序。其中一些常用标签包括: 1. `<welcome-file-list>`标签用于指定欢迎页面,即当用户访问Web应用程序时,默认显示的页面。可以在该标签内使用多个`<welcome-file>`标签来指定多个欢迎页面。 2. `<servlet>`标签用于配置Servlet组件。可以在该标签内指定Servlet的名称、类名、初始化参数等信息。 3. `<servlet-mapping>`标签用于将Servlet映射到URL模式。可以在该标签内指定Servlet的名称和URL模式,以便将特定的URL请求转发给相应的Servlet处理。 4. `<filter>`标签用于配置过滤器组件。过滤器可以在请求到达Servlet之前或响应返回给客户端之前对请求和响应进行处理。可以在该标签内指定过滤器的名称、类名、初始化参数等信息。 5. `<listener>`标签用于配置监听器组件。监听器可以监听Web应用程序中的事件,例如ServletContext的创建和销毁、Session的创建和销毁等。可以在该标签内指定监听器的类名。 这些标签可以在web.xml文件中使用,用于配置和初始化Web应用程序的各个组件和功能。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [Java Web web.xml标签<web-app>浅析](https://blog.csdn.net/u010857034/article/details/107409443)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [web.xml中各个标签的作用](https://blog.csdn.net/weixin_41761540/article/details/88072817)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值