筆者從大學畢業一直從事網上銀行的開發,都是一些web開發項目。接下來會寫一些關於web開發相關的東西,也是自己工作以來經常用到的內容。本篇先從web.xml文件開始介紹,筆者接觸到的項目中都有這個文件,這個文件是Tomcat工程中最基礎也是最重要的配置文件,Tomcat啟動項目的時候會加載並讀取這個文件,所以很有必要弄懂這個文件。
1、XML 聲明和根元素
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp"> …… </web-app>
和所有xml類型的文件一樣,第一行是 XML 聲明,它定義 XML 的版本 (1.0) 和所使用的編碼 (UTF-8)。
第二行的“DOCYTPE”聲明必須緊跟在XML 聲明之后,這個聲明會告訴服務器適用的servlet規范的版本(如2.3),並指定管理此文件其余部分內容的語法的DTD(Document Type Definition即文檔類型定義)。
第三行的<web-app></web-app>標簽就是此文件的根元素,web.xml文件的最主要的配置信息就包含在這個標簽之內,接下來重點介紹的就是這個標簽包含的內容。xml文件不僅對大小寫敏感,而且還對出現在其他元素中的次序敏感。所以標簽的大小寫一定要使用正確,而且XML聲明必須是文件中的第一項,DOCTYPE聲明必須是第二項,而web- app元素必須是第三項。同樣在web-app元素內,元素的次序也很重要。
2、Web 應用配置
<icon>
<small-icon>/images/small_icon.gif</small-icon>
<large-icon>/images/large_icon.gif</large-icon>
</icon>
<display-name>WEB</display-name>
<desciption>WEB Example</desciption>
<context-param>
<param-name>name</para-name>
<param-value>value</param-value>
<description>description</description>
</context-param>
<icon>標簽指出IDE和GUI工具用來表示Web應用的大圖標和小圖標的位置。
<display-name>標簽指出此Web應用的應用名稱。
<desciption>顧名思義就是對此Web應用的一些描述信息。
<context-param>聲明此應用范圍內的初始化參數,這些參數可在servlet類中通過getServletContext().getInitParameter(“name”)方法獲取。
3、filter過濾器
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern> /main.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/addProduct.jsp</url-pattern>
</filter-mapping>
<filter>標簽內部配置過濾器,<filter-name>標簽指定此過濾器的名字,<filter-class>標簽指定此過濾器指向的類(MyFilter),此類必須實現javax.servlet.Filter接口。<filter-mapping>標簽用來關聯一個或多個servlet或jsp頁面(/main.jsp和/addProduct.jsp)。注意無論有多少<filter-mapping>,他們的<filter-name>必須與前面的名字一致。
4、listener監聽器
<listener>
<listener-class>com.channel.http.SessionListener</listener-class>
</listener>
<listener>標簽指定監聽器,用於監聽Web應用中某些對象、信息的創建、銷毀、增加,修改,刪除等動作的發生,然后作出相應的響應處理。當范圍對象的狀態發生變化的時候,服務器自動調用監聽器對象中的方法。常用於統計在線人數和在線用戶,系統加載時進行信息初始化,統計網站的訪問量等等。
5、servlet(很重要)
<servlet> <servlet-name>yqzl</servlet-name> <servlet-class> com.channel.http.servlet.MainServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>yqzl</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
<servlet>標簽用於指定此Web應用的servlet相關配置,這個配置相當重要。<servlet-name>標簽指定此servlet的名字,<servlet-class>指定servlet的類,這個類開發者可以自己寫,一般會繼承HttpServlet類,用來初始化整個Web項目和接受http請求並處理。<init-param>標簽里面可以配置一些參數。
<load-on-startup>標簽指定當前Web應用啟動時裝載Servlet的次序,它的內容必須是整數,當這個數>=0時,容器會按數值從小到大依次加載。如果數值<0或沒有指定,容器將在用戶首次訪問時加載這個servlet類。<servlet-mapping>標簽可定義servlet映射,里面的<servlet-name>必須與前面的名字一致,<url-pattern>指定servlet映射的路徑。
在用Tomcat啟動整個web項目時,當配置了<load-on-startup>標簽並且里面的數字>=0時,會加載此servlet類,創建類的實例,調用init()方法初始化<init-param>標簽里面的配置信息,此初始化在整個servlet生命周期中只會進行一次。如果未配置<load-on-startup>標簽或數字<0時,Tomcat啟動時不會加載此servlet類,當然也就不會調用init()方法進行初始化,當用戶首次訪問時會加載類並初始化,所以此時第一次訪問時可能會加載很慢。這里僅簡單介紹標簽的作用,以后我會詳細介紹這個servlet類以及與其相關的幾個比較重要的類和接口。
6、session配置
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<session-config>標簽用於設置瀏覽器與服務器之間會話(交互)時長的間隔,以分鍾為單位。用戶訪問Web項目時,會與服務器之間建立一個session,當用戶長時間未與服務器信息交互而超過這個時長時,服務器會銷毀這個session,釋放占用的內存空間,當用戶再次在原來的session訪問時會失效,需要重新建立新的session。除了可以設置這個標簽之外,還可通過HttpSession的setMaxInactiveInterval()方法設置單個會話對象的超時值。
7、歡迎頁配置
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
當用戶訪問Web應用時,如果僅給出Web應用的根訪問URL,沒有指定具體的文件名,容器會調用<weblcome-file- list> 標簽里指定的文件。<welcome-file-list>里允許有多個<welcome-file>元素,每個元素代表一個文件。容器會按順序尋找,先找第一個文件是否存在,如果存在則會把這個文件顯示出來,不再尋找其他文件。如果不存在則尋找第二個文件,依次類推。如果所有文件都不存在,則拋出404錯誤。
8、其他配置
下面的這幾項配置,筆者在開發中目前還沒遇到過,不是特別了解,這里就不做過多解釋,以免引起錯誤。這些內容都是參考 https://www.cnblogs.com/hafiz/p/5715523.html 的內容,有興趣可以去這篇博客去看。
<mime-mapping></mime-mapping>
如果Web應用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。
<error-page></error-page>
在返回特定HTTP狀態代碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面。
<taglib></taglib>
對標記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置,而不用編輯使用這些文件的JSP頁面。
<resource-env-ref></resource-env-ref>
聲明與資源相關的一個管理對象。
<resource-ref></resource-ref>
聲明一個資源工廠使用的外部資源。
<security-constraint></security-constraint>
制定應該保護的URL。它與login-config元素聯合使用。
<login-config></login-config>
指定服務器應該怎樣給試圖訪問受保護頁面的用戶授權。它與sercurity-constraint元素聯合使用。
<security-role></security-role>
給出安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子元素中,分別地聲明角色可使高級IDE處理安全信息更為容易。
<env-entry></env-entry>
聲明Web應用的環境項。
<ejb-ref></ejb-ref>
聲明一個EJB的主目錄的引用。
<ejb-local-ref></ejb-local-ref>
聲明一個EJB的本地主目錄的應用。
以上基本上涵蓋了配置web.xml文件所需要的元素以及對這些元素簡單的介紹,不同元素配置的基本順序大致如此。其中<display-name>、<desciption>、<context-param>、<filter>、<listener>、<servlet>、<servlet-mapping>、<session-config>、<welcome-file-list>這幾項是筆者在開發過程中遇到最多的,尤其是和servlet相關的配置個人認為是重中之重。所以接下來的幾篇中我會詳細介紹servlet類以及與其相關的幾個比較重要的類和接口。這些內容也是筆者根據實際工作以及參考一些資料總結而來的,當然其中難免有些錯誤或者遺漏,歡迎大家指正,我也在不斷學習不斷提高。
下面是完整的web.xml文件配置。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app id="WebApp"> <icon> <small-icon>/images/small_icon.gif</small-icon> <large-icon>/images/large_icon.gif</large-icon> </icon> <display-name>WEB</display-name> <desciption>WEB Example</desciption> <context-param> <param-name>name</para-name> <param-value>value</param-value> <description>description</description> </context-param> <filter> <filter-name>MyFilter</filter-name> <filter-class>com.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern> /main.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/addProduct.jsp</url-pattern> </filter-mapping> <listener> <listener-class>com.channel.http.SessionListener</listener-class> </listener> <servlet> <servlet-name>yqzl</servlet-name> <servlet-class> com.channel.http.servlet.MainServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>yqzl</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <session-config> <session-timeout>10</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <mime-mapping></mime-mapping> <error-page></error-page> <taglib></taglib> <resource-env-ref></resource-env-ref> <resource-ref></resource-ref> <security-constraint></security-constraint> <login-config></login-config> <security-role></security-role> <env-entry></env-entry> <ejb-ref></ejb-ref> <ejb-local-ref></ejb-local-ref> </web-app>
轉載請注明出處 http://www.cnblogs.com/Y-oung/p/8401549.html
工作、學習、交流或有任何疑問,請聯系郵箱:yy1340128046@163.com 微信:yy1340128046