问题:在看项目的web.xml时,发现这两个标签,且,标签中的内容有重合部门,可是读取的对应的xml文件不一样。html
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 用来标记该web应用的名称 --> <display-name>contract</display-name> <!-- Spring和mybatis的配置文件 --><!-- classpath:activemq.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml classpath:spring-security.xml </param-value> </context-param> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring Security --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- Spring MVC servlet --> <servlet> <servlet-name>contract</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-mvc.xml classpath:spring-quartz.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>contract</servlet-name> <!-- 此处能够能够配置成*.do,对应struts的后缀习惯 --> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>httpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>httpMethodFilter</filter-name> <servlet-name>contract</servlet-name> </filter-mapping> <!-- 用于过滤request中的parameter,转换xss和sql注入攻击的特殊字符, 可是不会过滤上传文件--> <filter> <filter-name>xssFilter</filter-name> <filter-class>com.contract.base.mvc.filter.XssFilter</filter-class> </filter> <filter-mapping> <filter-name>xssFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <!-- <welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list> --> <!-- 配置SESSION超时,单位是分钟 --> <session-config> <session-timeout>540</session-timeout> <!-- 容器将使用华华cookie追踪回话ID --> <tracking-mode>COOKIE</tracking-mode> <cookie-config> <!-- 把cookie-secure的值改成了true,true意味着"指示浏览器仅经过 HTTPS 链接传回 cookie。这能够确保 cookie ID 是安全的,且仅用于使用 HTTPS 的网站。若是启用此功能,则 HTTP 上的会话 Cookie 将再也不起做用 --> <secure>false</secure> <!-- cookie设置了HttpOnly标志,能够在发生XSS时避免JavaScript读取cookie --> <http-only>true</http-only> </cookie-config> </session-config> </web-app>
首先我来来解释一个web.xml中配置<context-param>做用: 1.启动一个web项目的额时候,容器(如:tomcat)回去读它的配置文件web.xml。读两个点:<listener></listener> 和<context-param></context-param> 2.紧接着,容器建立一个ServletContext(上下文),这个项目全部的部分都讲共享这个上下文。 3.容器将<context-param></context-param>转化为键值对,并交给ServletContext. 4.容器建立<listener></listener>中的类实例,即建立监听。 5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中得到ServletContext = ServletContextEvent.getServletContext(); context-param的值 = ServletContext.getInitParameter("context-param的键"); 6.获得这个context-param的值以后,你就能够作一些操做了.注意,这个时候你的WEB项目尚未彻底启动完成.这个动做会比全部的Servlet都要早. 换句话说,这个时候,你对<context-param>中的键值作的操做,将在你的WEB项目彻底启动以前被执行. 7.举例.你可能想在项目启动以前就打开数据库. 那么这里就能够在<context-param>中设置数据库的链接方式,在监听类中初始化数据库的链接. 8.这个监听是本身写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.好比说数据库链接的关闭.java
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置以下: <context-param> <param-name>context/param</param-name> <param-value>avalible during application</param-value> </context-param> (2)servlet范围内的参数,只能在servlet的init()方法中取得web