javaWeb項目之簡析配置文件

javaWeb項目配置文件:

web.xml解析:(大小寫敏感,標籤不嵌套) context-param > listener  >  fileter  > servlet

  1. 在啓動Web項目時,容器(比如Tomcat)會讀web.xml配置文件中的兩個節點<listener>和<contex-param>。
  2. 接着容器會創建一個ServletContext(上下文),應用範圍內即整個WEB項目都能使用這個上下文。
  3. 接着容器會將讀取到<context-param>轉化爲鍵值對,並交給ServletContext。
  4. 容器創建<listener></listener>中的類實例,即創建監聽(備註:listener定義的類可以是自定義的類但必須需要繼承ServletContextListener)。
  5. 在監聽的類中會有一個contextInitialized(ServletContextEvent event)初始化方法,在這個方法中可以通過event.getServletContext().getInitParameter("contextConfigLocation") 來得到context-param 設定的值。在這個類中還必須有一個contextDestroyed(ServletContextEvent event) 銷燬方法.用於關閉應用前釋放資源,比如說數據庫連接的關閉。
  6. 得到這個context-param的值之後,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啓動完成.這個動作會比所有的Servlet都要早。

<?xml version="1.0" encoding="UTF-8"?>   必須,描述xml版本和文件的字符編碼

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">  定義標籤頭

<display-name>Archetype Created Web Application</display-name>:xml編輯器顯示的名稱

<context-param>標籤:可在項目啓動之前做一些操作(打開數據庫,加載配置文檔,聲明應用內初始化參數等  Application)

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-ehcache.xml,classpath:spring-hibernate.xml,classpath:spring-druid.xml</param-value>
</context-param>

<filter>標籤:設置過濾器,指定web容器的過濾器(自定義過濾器須實現 javax.servlet.Filter init(),doFilter()和destory()方法,init方法是在WEB應用啓動就會調用,doFilter則是在訪問filter-mapping映射到的url時會調用實現URL級別的權限訪問控制、過濾敏感詞彙、壓縮響應信息等,可根據用戶訪問方式配置調用(REQUEST,INCLUDE,FORWARD,ERROR)

<!-- 配置字符集過濾器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<!-- 配置項目的編碼mapping (用戶直接訪問的時候調用)-->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

<listener>標籤:捕捉到服務器的啓動和停止,在啓動和停止觸發裏面的方法做相應的操作,通過監聽器,可自動激發一些操作,如監聽在線用戶數量


<!-- 配置文件加載監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止內存溢出監聽器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<servlet>標籤:將servlet類與訪問的映射路徑關聯起來

<!-- 配置spring mvc -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

配置session-config,配置session失效時間

<session-config>
<session-timeout>120</session-timeout>
</session-config>

更多詳情見:最全web.xml配置

Spring.xml:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章