Java web.xml筆記
阿新 • • 發佈:2019-01-11
Javaweb專案中, web.xml檔案其中的各種設定, 就是簡單的標註
<?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"> <display-name>hujing</display-name> <!-- 歡迎頁面,從上到下依次尋找 --> <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> <!-- 將值放入ServletContext中 --> <context-param> <param-name>name</param-name> <param-value>value></param-value> </context-param> <!-- 註冊servlet --> <servlet> <servlet-name>aaa</servlet-name> <servlet-class>servlet.AServlet</servlet-class> <!-- 為這個Servlet的ServletConfig設定初始引數 --> <init-param> <param-name>name</param-name> <param-value>123</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>aaa</servlet-name> <!-- url可以有多種寫法,也可以有多個 --> <url-pattern>/son</url-pattern> <url-pattern>/son/nn</url-pattern> <url-pattern>/son/*</url-pattern> <!-- 攔截所有請求 --> <url-pattern>/*</url-pattern> <!-- 只會攔截靜態資源請求,不會攔截動態資源請求 --> <url-pattern>/</url-pattern> <!-- 字尾名攔截 --> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 註冊Filter --> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class> <!-- 設定引數會存到FilterConfig中 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>aaa</filter-name> <!-- url可以有多種寫法,也可以有多個,可參考Servlet,在Filter中'/'不起作用 --> <url-pattern>/son</url-pattern> <!-- 表示當前filter攔截指定servlet請求,寫了servlet-name就不寫url-pattern --> <servlet-name>aaa</servlet-name> <!-- 截斷的請求型別,可以指定多個 REQUEST: 截斷request請求(預設) FORWORD: 截斷forword請求 INCLUDE: 截斷include請求 ERROR: 截斷<error-page>請求 --> <dispatcher></dispatcher> </filter-mapping> <!-- 註冊出現錯誤時的跳轉頁面 --> <error-page> <!-- 通過錯誤碼配置 --> <error-code>404</error-code> <!-- 通過異常類配置 --> <exception-type>Java.lang.NullException</exception-type> <!-- 跳轉的頁面 --> <location>/servlet</location> </error-page> <!-- 註冊Servlet監聽器 --> <listener> <listener-class>類全名</listener-class> </listener> </web-app>