1. <web-app>
web-app元素是web应用程序部署描述符的根元素。这个元素的子元素可以是任意顺序的。
2. <display-name>
定义web应用的名称
3. <distributable>
告诉servlet/JSP容器,Web容器中部署的应用程序适合在分布式环境下运行。
4. <context-param>
上下文初始化参数.含有一对参数名和参数值,用作应用的Servlet上下文初始化参数,参数名在整个Web应用中必须是惟一的,在web应用的整个生命周期中上下文初始化参数都存在,任意的Servlet和jsp都可以随时随地访问它。
子元素:
- <param-name> : 参数名
- <param-value>: 参数值
- <description>: 参数说明
spring配置文件
配置Spring,必须需要<listener>
,而<context-param>
可有可无,如果在web.xml
中不写<context-param>
配置信息,默认的路径是/WEB-INF/applicationontext.xml
,在WEB-INF
目录下创建的xml文件的名称必须是applicationContext.xml
。如果是要自定义文件名可以在web.xml
里加入contextConfigLocation
这个context参数; 在<param-value>
</param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-configuration/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在不同环境下如何获取
- 在JSP网页中可以使用下列方法来取得:
${initParam.param_name}
- 在Servlet可以使用下列方法来获得:
String param_name=request.getServletContext().getInitParamter("param_name");
Servlet的ServletConfig
对象拥有该Servlet的ServletContext
的一个引用,所以可这样取得上下文初始化参数, getServletConfig().getServletContext().getInitParameter()
, 也可以在Servlet中直接调用getServletContext().getInitParameter()
,两者是等价的。
5. <session-config>
设置容器的session参数.
子元素
- <session-timeout>
定义session会话的超时时间, 设置为0或负数则表示永不超时, 单位为分钟. - <cookie-config>
定义session跟踪的cookie配置.
子元素:- <comment> :说明
- <http-only> : 会话跟踪cookie是否将标记为HttpOnly
- <max-age>: 会话跟踪cookie的生存期(以秒为单位)
- <domain>
- <name>: 会话跟踪cookie的名称。缺省值是JSESSIONID
- <path>cookie的路径
- <secure> 会话跟踪cookie是否将被标记为安全的
- <tracking-mode>
定义了session会话的跟踪模式. 有三个选项: COOKIE, URL, SSL
6. <listener>
为web应用程序定义监听器,用来监听各种事件,比如:application和session事件,所有的监听器按照相同的方式定义,功能取决去它们各自实现的接口,常用的Web事件接口有如下几个:
- ServletContextListener:
监听Web应用的启动和关闭. - ServletContextAttributeListener:
用于监听ServletContext范围(application)内属性的改变. - ServletRequestListener:
监听用户的请求 - ServletRequestAttributeListener:
监听ServletRequest范围(request)内属性的改变; - HttpSessionListener:
监听用户session的开始和结束; - HttpSessionAttributeListener:
监听HttpSession范围(session)内属性的改变。
配置
配置Listener只要向Web应用注册Listener实现类即可,无序配置参数之类的东西,因为Listener获取的是Web应用ServletContext(application)的配置参数。为Web应用配置Listener的两种方式:
- 使用@WebListener修饰Listener实现类即可。
- 在web.xml文档中使用<listener>进行配置。
子元素
- <listener-class> : Listener的实现类(全限定类名).
7. <filter>
过滤器, 其功能是对用户请求request进行预处理,对Response进行后处理.使用Filter的完整流程是:Filter对用户请求进行预处理,接着将请求HttpServletRequest交给Servlet进行处理并生成响应,最后Filter再对服务器响应HttpServletResponse进行后处理。Filter与Servlet具有完全相同的生命周期,且Filter也可以通过<init-param>来配置初始化参数,获取Filter的初始化参数则使用FilterConfig的getInitParameter()。
配置
Filter必须实现javax.servlet.Filter
接口.在该接口中定义了三个方法:
- void init(FilterConfig config):
用于完成Filter的初始化。FilteConfig用于访问Filter的配置信息。 - void destroy():
用于Filter销毁前,完成某些资源的回收。 - void doFilter(ServletRequest request, ServletResponse response, FilterChain chain):
过滤功能的核心方法,对每个请求及响应增加额外的处理。它们的分界线为是否调用了chain.doFilter(request,response)
,执行该方法之前,即对用户请求request进行预处理,执行该方法之后,即对服务器响应response进行后处理。
子元素
- <filter-name>
定义过滤器的名称,该名称在整个程序中都必须唯一。 - <filter-class>
指定过滤器类的完全限定的名称 - <init-param>
为Filter配置参数,与<context-param>具有相同的元素描述符<param-name>和<param-value>
<filter-mapping>
指定Filter所拦截的URL。必须与其对应的<filter>具有相同的<filter-name>.
常用过滤器
- 字符集过滤器:
org.springframework.web.filter.CharacterEncodingFilter
.
8. <servlet>
Servlet是个特殊的java类,继承于javax.servlet.http.HttpServlet
.
子元素
- <servlet-name>:
定义servlet的名称,该名称在整个应用中必须是惟一的。 - <servlet-class>:
指定servlet的完全限定的名称。 - <jsp-file>
指定应用中JSP文件的完整路径。这个完整路径必须由/开始。 - <load-on-startup>
内容可以为空,或者是一个整数。这个值表示由Web容器载入内存的顺序。如果有两个Servlet元素都含有<load-on-startup>子元素,则<load-on-startup>子元素值较小的Servlet将先被加载。如果是正整数或0, 表示启动容器时,初始化Servlet。如果值是负整数,或者元素不存在,容器可以在它选择的任何时候加载servlet。 - <init-param> 初始化参数
- <multipart-config> 文件上传参数
- <file-size-threshold>
将上传文件写入磁盘的大小阈值 - <location>
文件存储路径 - <max-file-size>
上传文件的最大大小限制 - <max-request-size>
请求的multipart/form-data最大大小
- <file-size-threshold>
<servlet-mapping>
含有<servlet-name>和<url-pattern>, 指定servlet所处理的请求路径.
9. <welcome-file-list>
包含一个子元素<welcome-file>,<welcome-file>用来指定首页文件名称。可以包含一个或多个<welcome-file>子元素。如果在第一个<welcome-file>元素中没有找到指定的文件,Web容器就会尝试显示第二个,以此类推。
9. <env-entry>
定义JNDI环境变量
子元素
- <env-entry-name> 变量名
- <env-entry-value> 变量值
- <env-entry-type> 变量类型
附: web.xml示例
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!-- ========================================================== -->
<!-- General -->
<!-- ========================================================== -->
<!-- 定义应用名称 -->
<display-name>Example App</display-name>
<description>一个示例应用程序,适用于处理Tomcat的一些特性</description>
<!-- 是否支持分布式 -->
<distributable />
<!-- session超时时间 -->
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<!-- ========================================================== -->
<!-- 自定义标签库 -->
<!-- ========================================================== -->
<!-- 自JSP 2.0以来不再需要Taglib声明,请参见从web.xml中删除Taglib -->
<!-- 注意,每个web.xml只能有一个<jsp-config>元素 -->
<!-- ========================================================== -->
<!-- JSP 配置 -->
<!-- ========================================================== -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<include-prelude>/WEB-INF/jspf/prelude1.jspf</include-prelude>
<include-coda>/WEB-INF/jspf/coda1.jspf</include-coda>
</jsp-property-group>
</jsp-config>
<!-- ========================================================== -->
<!-- Context 参数 -->
<!-- ========================================================== -->
<context-param>
<description>Enable debugging for the application</description>
<param-name>debug</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>The email address of the administrator, used to send error reports.</description>
<param-name>webmaster</param-name>
<param-value>address@somedomain.com</param-value>
</context-param>
<!-- ========================================================== -->
<!-- JNDI 环境变量 -->
<!-- ========================================================== -->
<env-entry>
<env-entry-name>webmasterName</env-entry-name>
<env-entry-value>Ms. W. Master</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
<env-entry>
<env-entry-name>cms/defaultUserSettings/recordsPerPage</env-entry-name>
<env-entry-value>30</env-entry-value>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>
<env-entry>
<env-entry-name>cms/enableXMLExport</env-entry-name>
<env-entry-value>false</env-entry-value>
<env-entry-type>java.lang.Boolean</env-entry-type>
</env-entry>
<env-entry>
<env-entry-name>cms/enableEmailNotifications</env-entry-name>
<env-entry-value>true</env-entry-value>
<env-entry-type>java.lang.Boolean</env-entry-type>
</env-entry>
<!-- ========================================================== -->
<!-- Servlets -->
<!-- ========================================================== -->
<!-- 简单 Servlet, provide a name, class, description and map to URL /servlet/SimpleServlet -->
<servlet>
<servlet-name>Simple</servlet-name>
<servlet-class>SimpleServlet</servlet-class>
<description>This is a simple Hello World servlet</description>
</servlet>
<servlet-mapping>
<servlet-name>Simple</servlet-name>
<url-pattern>/servlet/SimpleServlet</url-pattern>
</servlet-mapping>
<!-- CMS Servlet, responds to *.cms URL's -->
<servlet>
<!-- Identification -->
<servlet-name>cms</servlet-name>
<servlet-class>com.metawerx.servlets.ContentManagementSystem</servlet-class>
<description>This servlet handles requests for the CMS (it is a controller in an MVC architecture)</description>
<!-- This servlet has two parameters -->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<!-- 容器启动时加载servlet (调用这个servlet的init()) -->
<load-on-startup>5</load-on-startup>
<!-- <run-at>0:00, 6:00, 12:00, 18:00</run-at> 这个标签仅在使用Resin服务器时有效 -->
</servlet>
<!-- 映射一些url到cms Servlet (demonstrates *.extension mapping) -->
<servlet-mapping>
<!-- cms servlet将会处理任何以.cms结尾的url -->
<servlet-name>cms</servlet-name>
<url-pattern>*.cms</url-pattern>
</servlet-mapping>
<!-- Rewriter Servlet, responds to /content/* and /admin/RewriterStatistics URL's -->
<!-- Define a servlet to respond to /content/* URL's -->
<servlet>
<servlet-name>rewriter</servlet-name>
<servlet-class>com.metawerx.servlets.URLRewriter</servlet-class>
</servlet>
<!-- Map some URL's to the rewriter servlet (demonstrates /path/* and specific URL mapping) -->
<servlet-mapping>
<!-- For any URL starting with /content/, the rewriter servlet will be called -->
<servlet-name>rewriter</servlet-name>
<url-pattern>/content/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<!-- The rewriter servlet can also be called directly as /admin/RewriterStatistics, to return stats -->
<servlet-name>rewriter</servlet-name>
<url-pattern>/admin/RewriterStatistics</url-pattern>
</servlet-mapping>
<!-- PathJSP Servlet, maps /shop/item/* URL's to a JSP file -->
<!-- Define a JSP file to respond to /shop/item/* URL's -->
<servlet>
<servlet-name>pathjsp</servlet-name>
<jsp-file>pathfinder.jsp</jsp-file>
</servlet>
<!-- Map some URL's to the pathjsp servlet (demonstrates /long/path/* URL mapping) -->
<servlet-mapping>
<!-- For any URL starting with /shop/item/, the pathjsp servlet will be called -->
<servlet-name>pathjsp</servlet-name>
<url-pattern>/shop/item/*</url-pattern>
</servlet-mapping>
<!-- ========================================================== -->
<!-- 过滤器 -->
<!-- ========================================================== -->
<!-- Example filter to set character encoding on each request (from Tomcat servlets-examples context) -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC_JP</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Example filter to dump the HTTP request at the top of each page (from Tomcat servlets-examples context) -->
<filter>
<filter-name>Request Dumper Filter</filter-name>
<filter-class>filters.RequestDumperFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Request Dumper Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ========================================================== -->
<!-- 监听器 -->
<!-- ========================================================== -->
<!-- Define example application events listeners -->
<listener>
<listener-class>com.metawerx.listener.ContextListener</listener-class>
</listener>
<listener>
<listener-class>com.metawerx.listener.SessionListener</listener-class>
</listener>
<!-- ========================================================== -->
<!-- Security -->
<!-- ========================================================== -->
<!-- 定义角色 -->
<security-role>
<role-name>admin</role-name>
</security-role>
<security-role>
<role-name>cms_editors</role-name>
</security-role>
<!-- Define a constraint to restrict access to /private/* -->
<security-constraint>
<display-name>Security constraint for the /private folder</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/private/*</url-pattern>
<!-- If you list http methods, only those methods are protected. -->
<!-- Leave this commented out to protect all access -->
<!--
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
-->
</web-resource-collection>
<auth-constraint>
<!-- Only only administrator and CMS editors to access this area -->
<role-name>admin</role-name>
<role-name>cms_editors</role-name>
</auth-constraint>
</security-constraint>
<!-- FORM based authentication -->
<!-- Leave this commented out, we will use BASIC (HTTP) authentication instead -->
<!--
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
-->
<!-- This application uses BASIC authentication -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Editor Login</realm-name>
</login-config>
<!-- Define a constraint to force SSL on all pages in the application -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Entire Application</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<!-- ========================================================== -->
<!-- 错误页面 -->
<!-- ========================================================== -->
<!-- Define an error handler for 404 pages -->
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
<!-- Define an error handler for java.lang.Throwable -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorThrowable.jsp</location>
</error-page>
<!-- ========================================================== -->
<!-- Extra MIME types -->
<!-- ========================================================== -->
<!-- Set XML mime-mapping so spreadsheets open properly instead of being sent as an octet/stream -->
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
<!-- ========================================================== -->
<!-- Locale -->
<!-- ========================================================== -->
<!-- Set Locale Encoding -->
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>ja</locale>
<encoding>Shift_JIS</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
<!-- ========================================================== -->
<!-- Welcome Files -->
<!-- ========================================================== -->
<!-- Define, in order of preference, which file to show when no filename is defined in the path -->
<!-- eg: when user goes to http://yoursite.com/ or http://yoursite.com/somefolder -->
<!-- Defaults are provided in the server-wide web.xml file, such as index.jsp, index.htm -->
<!-- Note: using this tag overrides the defaults, so don't forget to add them here -->
<welcome-file-list>
<!-- Use index.swf if present, or splash.jsp, otherwise just look for the normal defaults -->
<welcome-file>index.swf</welcome-file>
<welcome-file>splash.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
网友评论