一、web.xml是什么?
首先 web.xml 是java web 项目的一个重要的配置文件,但是web.xml文件并不是Java web工程必须的。
web.xml文件是用来配置:欢迎页、servlet、filter等的。当你的web工程没用到这些时,你可以不用web.xml文件来配置你的web工程。
所在位置 项目名/web/WEB-INFO/web.xml,如下图所示
web.xml能做的事情:
其实,web.xml的模式(Schema)文件中定义了多少种标签元素,web.xml中就可以出现它的模式文件所定义的标签元素,它就能拥有定义出来的那些功能。
web.xml的模式文件是由Sun公司定义的,每个web.xml文件的根元素
web.xml 配置文件 具体的内容
二、Web.xml详解:
1.web.xml加载过程(步骤)
首先简单讲一下,web.xml的加载过程。当启动一个WEB项目时,容器包括(JBoss、Tomcat等)首先会读取项目web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常地被启动起来。
1. 启动WEB项目的时候,容器首先会去它的配置文件web.xml读取两个节点:
2. 紧接着,容器创建一个ServletContext(application),这个WEB项目所有部分都将共享这个上下文。
3. 容器以
4. 容器创建
- <span style="font-family:Times New Roman;">ServletContextapplication=ServletContextEvent.getServletContext();span>
context-param的值就是application.getInitParameter("context-param的键");得到这个context-param的值之后,你就可以做一些操作了。
5. 举例:你可能想在项目启动之前就打开数据库,那么这里就可以在
6. 接着,容器会读取
7. 以上都是在WEB项目还没有完全启动起来的时候就已经完成了的工作。如果系统中有Servlet,则Servlet是在第一次发起请求的时候被实例化的,而且一般不会被容器销毁,它可以服务于多个用户的请求。所以,Servlet的初始化都要比上面提到的那几个要迟。
8. 总的来说,web.xml的加载顺序是:
9. 对于某类元素而言,与它们出现的顺序是有关的。以
2.web.xml标签详解
1. XML文档有效性检查
- <span style="font-family:Times New Roman;">>span>
这段代码指定文件类型定义(DTD),可以通过它检查XML文档的有效性。下面显示的元素有几个特性,这些特性告诉我们关于DTD的信息:
- web-app定义该文档(部署描述符,不是DTD文件)的根元素
- PUBLIC意味着DTD文件可以被公开使用
- -//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”意味着DTD由Sun Microsystems, Inc.维护。该信息也表示它描述的文档类型是DTD Web Application 2.3,而且DTD是用英文书写的。
- URL"http://java.sun.com/dtd/web-app_2_3.dtd"表示D文件的位置。
2.
部署描述符的根元素是
- <span style="font-family:Times New Roman;">> span>
正如您所看到的,这个元素含有23个子元素,而且子元素都是可选的。问号(?)表示子元素是可选的,而且只能出现一次。星号(*)表示子元素可在部署描述符中出现零次或多次。有些子元素还可以有它们自己的子元素。web.xml文件中
注意:在Servlet 2.3中,子元素必须按照DTD文件语法描述中指定的顺序出现。比如:如果部署描述符中的
3.
4.
5.
-
-
<context-param>
-
<param-name>webAppRootKeyparam-name>
-
<param-value>business.rootparam-value>
-
context-param>
-
-
<context-param>
-
<param-name>contextConfigLocationparam-name>
-
<param-value>/WEB-INF/spring-configuration/*.xmlparam-value>
-
context-param>
5.1解释:
5.2 什么情况下使用,为什么使用:
5.3 Spring配置文件:
-
-
<context-param>
-
<param-name>contextConfigLocationparam-name>
-
<param-value>/WEB-INF/spring-configuration/*.xmlparam-value>
-
context-param>
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
-
listener>
-
-
<context-param>
-
<param-name>webAppRootKeyparam-name>
-
<param-value>webapp.rootparam-value>
-
context-param>
-
<!—business-client应用路径 -->
-
<context-param>
-
<param-name>webAppRootKeyparam-name>
-
<param-value> business.root param-value>
-
context-param>
-
<!—public-base应用路径 -->
-
<context-param>
-
<param-name>webAppRootKeyparam-name>
-
<param-value> pubbase.rootparam-value>
-
context-param>
5.4 多个配置文件交叉引用处理:
-
<context-param>
-
<param-name>contextConfigLocation param-name>
-
<param-value> applicationContext-database.xml,applicationContext.xmlparam-value>
-
<context-param>
-
<bean id="userService" class="domain.user.service.impl.UserServiceImpl">
-
<property name="dbbean">
-
<ref bean="dbBean"/>
-
property>
-
bean>
5.5 在不同环境下如何获取:范例:
-
<context-param>
-
<param-name>param_nameparam-name>
-
<param-value>param_valueparam-value>
-
context-param>
6.
-
-
<session-config>
-
<session-timeout>120session-timeout>
-
session-config>
7.
-
-
-
<listener>
-
<listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
-
listener>
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
-
listener>
-
-
<listener>
-
<listener-class>com.yonyou.mcloud.cas.client.session.SingleSignOutHttpSessionListenerlistener-class>
-
listener>
7.1 Listener介绍:
(1). ServletContextListener:用于监听Web应用的启动和关闭;
(2). ServletContextAttributeListener:用于监听ServletContext范围(application)内属性的改变;
(3). ServletRequestListener:用于监听用户的请求;
(4). ServletRequestAttributeListener:用于监听ServletRequest范围(request)内属性的改变;
(5). HttpSessionListener:用于监听用户session的开始和结束;
(6). HttpSessionAttributeListener:用于监听HttpSession范围(session)内属性的改变。
7.2 Listener配置:
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
-
listener>
-
<servlet>
-
<servlet-name>contextservlet-narne>
-
<servlet-class>org.springframework.web.context.ContextLoaderServletservlet-class>
-
<load-on-startup>1load-on-startup>
-
servlet>
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
-
listener>
8.
-
-
-
<filter>
-
<filter-name>CharacterEncodingFilterfilter-name>
-
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
-
<init-param>
-
<param-name>encodingparam-name>
-
<param-value>UTF-8param-value>
-
init-param>
-
<init-param>
-
<param-name>forceEncodingparam-name>
-
<param-value>trueparam-value>
-
init-param>
-
filter>
-
-
<filter>
-
<filter-name>CAS Single Sign Out Filterfilter-name>
-
<filter-class>com.yonyou.mcloud.cas.client.session.SingleSignOutFilterfilter-class>
-
filter>
-
-
<filter>
-
<filter-name>CAS Authentication Filterfilter-name>
-
<filter-class>com.yonyou.mcloud.cas.client.authentication.ExpandAuthenticationFilterfilter-class>
-
<init-param>
-
<param-name>casServerLoginUrlparam-name>
-
<param-value>https://dev.yonyou.com:443/sso-server/loginparam-value>
-
init-param>
-
<init-param>
-
-
<param-name>serverNameparam-name>
-
<param-value>http://10.1.215.40:80param-value>
-
init-param>
-
filter>
-
-
<filter>
-
<filter-name>CAS Validation Filterfilter-name>
-
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilterfilter-class>
-
<init-param>
-
<param-name>casServerUrlPrefixparam-name>
-
<param-value>https://dev.yonyou.com:443/sso-serverparam-value>
-
init-param>
-
<init-param>
-
<param-name>serverNameparam-name>
-
<param-value>http://10.1.215.40:80param-value>
-
init-param>
-
<init-param>
-
<param-name>proxyCallbackUrlparam-name>
-
<param-value>https://dev.yonyou.com:443/business/proxyCallbackparam-value>
-
init-param>
-
<init-param>
-
<param-name>proxyReceptorUrlparam-name>
-
<param-value>/proxyCallbackparam-value>
-
init-param>
-
<init-param>
-
<param-name>proxyGrantingTicketStorageClassparam-name>
-
<param-value>com.yonyou.mcloud.cas.client.proxy.MemcachedBackedProxyGrantingTicketStorageImplparam-value>
-
init-param>
-
-
<init-param>
-
<param-name>encodingparam-name>
-
<param-value>UTF-8param-value>
-
init-param>
-
filter>
-
<filter>
-
<filter-name>CAS HttpServletRequest Wrapper Filterfilter-name>
-
<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilterfilter-class>
-
filter>
-
<filter>
-
<filter-name>CAS Assertion Thread Local Filterfilter-name>
-
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilterfilter-class>
-
filter>
-
<filter>
-
<filter-name>NoCache Filterfilter-name>
-
<filter-class>com.yonyou.mcloud.cas.client.authentication.NoCacheFilterfilter-class>
-
filter>
-
-
<filter-mapping>
-
<filter-name>CharacterEncodingFilterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
-
<filter-mapping>
-
<filter-name>NoCache Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
-
<filter-mapping>
-
<filter-name>CAS Single Sign Out Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
-
<filter-mapping>
-
<filter-name>CAS Validation Filterfilter-name>
-
<url-pattern>/proxyCallbackurl-pattern>
-
filter-mapping>
-
<filter-mapping>
-
<filter-name>CAS Authentication Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
-
<filter-mapping>
-
<filter-name>CAS Validation Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
-
<filter-mapping>
-
<filter-name>CAS HttpServletRequest Wrapper Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
-
<filter-mapping>
-
<filter-name>CAS Assertion Thread Local Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
8.1 Filter介绍:
(1) 创建Filter处理类
(2) Web.xml文件中配置Filter
Filter必须实现javax.servlet.Filter接口,在该接口中定义了三个方法:
(1) void init(FilterConfig config):用于完成Filter的初始化。FilteConfig用于访问Filter的配置信息。
(2) void destroy():用于Filter销毁前,完成某些资源的回收。
(3) void doFilter(ServletRequest request,ServletResponse response,FilterChain chain):实现过滤功能的核心方法,该方法就是对每个请求及响应增加额外的处理。该方法实现对用户请求request进行预处理,也可以实现对服务器响应response进行后处理---它们的分界线为是否调用了chain.doFilter(request,response),执行该方法之前,即对用户请求request进行预处理,执行该方法之后,即对服务器响应response进行后处理。
8.2 Filter配置:
Filter可认为是Servlet的“增强版”,因此Filter配置与Servlet的配置非常相似,需要配置两部分:配置Filter名称和Filter拦截器URL模式。区别在于Servlet通常只配置一个URL,而Filter可以同时配置多个请求的URL。配置Filter有两种方式:(1). 在Filter类中通过Annotation进行配置。
我们使用的是web.xml这种配置方式,下面重点介绍
8.21 字符集过滤器
-
<filter>
-
<filter-name>CharacterEncodingFilterfilter-name>
-
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
-
<init-param>
-
<param-name>encodingparam-name>
-
<param-value>UTF-8param-value>
-
init-param>
-
<init-param>
-
<param-name>forceEncodingparam-name>
-
<param-value>trueparam-value>
-
init-param>
-
filter>
-
<filter-mapping>
-
<filter-name>CharacterEncodingFilterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
8.22 缓存控制
-
<filter>
-
<filter-name>NoCache Filterfilter-name>
-
<filter-class>com.yonyou.mcloud.cas.client.authentication.NoCacheFilterfilter-class>
-
filter>
-
<filter-mapping>
-
<filter-name>NoCache Filterfilter-name>
-
<!—表示对URL全部过滤-->
-
<url-pattern>/*url-pattern>
-
filter-mapping>
8.23 登录认证
-
-
<filter>
-
<filter-name>CAS Authentication Filterfilter-name>
-
<filter-class>com.yonyou.mcloud.cas.client.authentication.ExpandAuthenticationFilterfilter-class>
-
<init-param>
-
<param-name>casServerLoginUrlparam-name>
-
<param-value>https://dev.yonyou.com:443/sso-server/loginparam-value>
-
init-param>
-
<init-param>
-
-
<param-name>serverNameparam-name>
-
<param-value>http://10.1.215.40:80param-value>
-
init-param>
-
filter>
-
<filter-mapping>
-
<filter-name>CAS Authentication Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
8.24 单点登出
-
<filter>
-
<filter-name>CAS Single Sign Out Filterfilter-name>
-
<filter-class>org.jasig.cas.client.session.SingleSignOutFilterfilter-class>
-
filter>
-
<filter-mapping>
-
<filter-name>CAS Single Sign Out Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
-
<listener>
-
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListenerlistener-class>
-
listener>
8.25 封装request
-
<filter>
-
<filter-name>CAS HttpServletRequest Wrapper Filterfilter-name>
-
<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilterfilter-class>
-
filter>
-
<filter-mapping>
-
<filter-name>CAS HttpServletRequest Wrapper Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
8.26 存放Assertion到ThreadLocal中
-
<filter>
-
<filter-name>CAS Assertion Thread Local Filterfilter-name>
-
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilterfilter-class>
-
filter>
-
<filter-mapping>
-
<filter-name>CAS Assertion Thread Local Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
8.27 禁用浏览器缓存
-
<filter>
-
<filter-name>NoCache Filterfilter-name>
-
<filter-class>com.yonyou.mcloud.cas.client.authentication.NoCacheFilterfilter-class>
-
filter>
-
<filter-mapping>
-
<filter-name>NoCache Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
8.28 CAS Client向CAS Server进行ticket验证
-
-
<filter>
-
<filter-name>CAS Validation Filterfilter-name>
-
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilterfilter-class>
-
<init-param>
-
<param-name>casServerUrlPrefixparam-name>
-
<param-value>https://dev.yonyou.com:443/sso-serverparam-value>
-
init-param>
-
<init-param>
-
<param-name>serverNameparam-name>
-
<param-value>http://10.1.215.40:80param-value>
-
init-param>
-
<init-param>
-
<param-name>proxyCallbackUrlparam-name>
-
<param-value>https://dev.yonyou.com:443/business/proxyCallbackparam-value>
-
init-param>
-
<init-param>
-
<param-name>proxyReceptorUrlparam-name>
-
<param-value>/proxyCallbackparam-value>
-
init-param>
-
<init-param>
-
<param-name>proxyGrantingTicketStorageClassparam-name>
-
<param-value>com.yonyou.mcloud.cas.client.proxy.MemcachedBackedProxyGrantingTicketStorageImplparam-value>
-
init-param>
-
-
<init-param>
-
<param-name>encodingparam-name>
-
<param-value>UTF-8param-value>
-
init-param>
-
filter>
-
<filter-mapping>
-
<filter-name>CAS Validation Filterfilter-name>
-
<url-pattern>/proxyCallbackurl-pattern>
-
filter-mapping>
-
<filter-mapping>
-
<filter-name>CAS Validation Filterfilter-name>
-
<url-pattern>/*url-pattern>
-
filter-mapping>
9.
-
-
-
<servlet>
-
<servlet-name>businessservlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
<init-param>
-
<param-name>publishContextparam-name>
-
<param-value>falseparam-value>
-
init-param>
-
<load-on-startup>1load-on-startup>
-
servlet>
-
-
<servlet>
-
<servlet-name>LogOutServletservlet-name>
-
<servlet-class>com.yonyou.mcloud.cas.web.servlet.LogOutServletservlet-class>
-
<init-param>
-
<param-name>serverLogoutUrlparam-name>
-
<param-value>https://dev.yonyou.com:443/sso-server/logoutparam-value>
-
init-param>
-
<init-param>
-
<param-name>serverNameparam-name>
-
<param-value>http://10.1.215.40:80/business/param-value>
-
init-param>
-
servlet>
-
-
<servlet-mapping>
-
<servlet-name>LogOutServletservlet-name>
-
<url-pattern>/logouturl-pattern>
-
servlet-mapping>
-
<servlet-mapping>
-
<servlet-name>businessservlet-name>
-
<url-pattern>/url-pattern>
-
servlet-mapping>
9.1 Servlet介绍:
Servlet通常称为服务器端小程序,是运行在服务器端的程序,用于处理及响应客户的请求。Servlet是个特殊的java类,继承于HttpServlet。客户端通常只有GET和POST两种请求方式,Servlet为了响应则两种请求,必须重写doGet()和doPost()方法。大部分时候,Servlet对于所有的请求响应都是完全一样的,此时只需要重写service()方法即可响应客户端的所有请求。9.2 Servlet配置:
为了让Servlet能响应用户请求,还必须将Servlet配置在web应用中,配置Servlet需要修改web.xml文件。从Servlet3.0开始,配置Servlet有两种方式:9.3DispatcherServlet在web.xml中的配置:
-
-
<servlet>
-
<servlet-name>businessservlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
<init-param>
-
<param-name>publishContextparam-name>
-
<param-value>falseparam-value>
-
init-param>
-
<load-on-startup>1load-on-startup>
-
servlet>
(2).在public-base-server工程中,通过第2种方式进行配置,把spring-servlet.xml放到src/main/resources/config/spring-servlet.xml,则需要在配置DispatcherServlet时指定
-
<servlet>
-
<servlet-name>springservlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
<init-param>
-
<param-name>publishContextparam-name>
-
<param-value>falseparam-value>
-
init-param>
-
<init-param>
-
<param-name>contextConfigLocationparam-name>
-
<param-value>classpath:config/spring-servlet.xmlparam-value>
-
init-param>
-
<load-on-startup>1load-on-startup>
-
servlet>
9.4 ContextLoaderListener和DispatcherServlet初始化上下文关系和区别:
10.
-
-
<welcome-file-list>
-
<welcome-file>index.htmlwelcome-file>
-
welcome-file-list>
11. 参考文献:
https://blog.csdn.net/ahou2468/article/details/79015251