問題:在看項目的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