關於Tomcat的web.xml檔案配置一些總結
阿新 • • 發佈:2019-02-17
0x00 前言
在JSP中,我們可以使用web.xml來定義Servlet,web.xml檔案稱為部署描述檔案(Deployment Descriptor,簡稱DD檔案),該檔案可以覆蓋Servelt中的標識設定,所以總結一下該檔案的標籤元素以及作用(一個工程不是必須要有web.xml檔案)。
0x01 web.xml檔案載入過程
<context-param>-> <listener> -> <filter> -> <servlet>
同個型別之間的實際程式呼叫的時候的順序是根據對應的 mapping 的順序進行呼叫
0x02 web.xml檔案元素
2.1 模式檔案
web.xml的模式檔案是由Sun公司定義的,每一個web.xml檔案的根元素中都標明.xml使用哪個模式檔案,其他元素放在中。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
2.2 指定歡迎頁面
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
這裡指定兩個歡迎頁面,按順序從第一個找起,如果第一個存在,訪問第一個,否則,訪問第二個。
2.3 給Servlet命名
<servlet> <servlet-name>jsp</servlet-name> <servlet-class>demo.JspServlet</servlet-class> </servlet>
將demo包下的JspServlet類命名為jsp.
2.4 給Servlet定製URL
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>hello.do</url-pattern>
</servlet-mapping>
通過在瀏覽器輸入localhost:8080/hello.do
就能匹配到該名為jsp的Servlet(上例就可以通過jsp匹配到JspServlet)。
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
任何副檔名為.do(檔名和路徑任意)的url請求都會匹配
localhost:8080/demo.do
localhost:8080/hello.do
2.5 定製初始化引數
可以定製servlet、JSP、Context的初始化引數,然後可以在servlet、JSP、Context中獲取這些引數值
<servlet>
<servlet-name>name01</servlet-name>
<servlet-class>demo.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
- servlet 配置Servlet
- servlet-name 定義Servlet的名字
- servlet-class 指定實現這個servlet的類
- init-param 定義Servlet的初始化引數和引數值,可有多個init-param
- load-on-startup 指定當Web應用啟動時,裝載Servlet的次序
- 當值為正數或零時,Servlet容器先載入數值小的servlet,再依次載入其他數值大的servlet
經過上面的配置,在Servlet中就能通過呼叫getServletConfig().getInitParameter("param1")
獲得引數名對應的值。
2.6 指定錯誤處理頁面
“錯誤碼”來指定錯誤處理頁面
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
“異常型別”來指定錯誤處理頁面
<error-page>
<exception-type>java.lang.Exception<exception-type>
<location>/exception.jsp<location>
</error-page>
2.7 配置session的有效時間
<session-config>
<session-timeout>30</session-timeout>
</session-config>
這裡預設是30分鐘
2.8 mime-type對映
<mime-mapping>
<extension>zip</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>
避免在瀏覽器直接開啟
2.9 設定監聽器
<listener>
<listener-class>demo.demoLisenet</listener-class>
</listener>
2.10 設定過濾器
設定一個編碼過濾器,過濾所有資源
<filter>
<filter-name>XCharaSetFilter</filter-name>
<filter-class>demo.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XCharaSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.11 定義了WEB應用的名字
<display-name>Test</display-name>
2.12 宣告WEB應用的描述資訊
<description>This is a test Page</description>
2.13 宣告應用範圍內的初始化引數
<context-param>
<param-name>web</param-name>
<param-value>log4</param-value>
</context-param>
取得值得方法:
ServletContext app =ServletContextEvent.getServletContext();
context-paramValue= app.getInitParameter("web");
- context-paramValue context-param的值
- web context-param的鍵,上面定義的
0x03 小結
XML文件的樣式與內容分離,可以幫我們快速尋找並提取有用的資料資訊,所以學一些關於XML的配置還有很有用的,這裡的只是通過在網上收集,還有Tomcat中預設的web.xml檔案中一些常見的元素,以後要用到時再查閱資料。