web.xml的作用
PS:每一個javaWeb工程都有一個web.xml配置文件,那麼他到底有什麼作用呢?它是每一個web工程都必的必須的嗎?
web.xml文件是用來初始化工程配置信息的,比如說welcome頁面,filter,listener,servlet,servlet-mapping,啓動加載級別等等,當你的web工程中沒用到這些當然也就不需要這個xml文件來配置你的apllication了
每一個xml文件都有定義他書寫規範的schema文件,web.xml所對應的xml Schema文件中定義了多少種標籤元素,web.xml中就可以出現它所定義的標籤元素,也就具備哪些特定的功能。web.xml的模式文件是由Sun 公司定義的,每個web.xml文件的根元素爲<web-app>中,必須標明這個web.xml使用的是哪個模式文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
</web-app>
web.xml的模式文件中定義的標籤並不是定死的,模式文件也是可以改變的,一般來說,隨着web.mxl模式文件的版本升級,裏面定義的功能會越來越複雜,標籤元素的種類肯定也會越來越多,但有些不是很常用的,我們只需記住一些常用的並知道怎麼配置就可以了。
下面我們介紹一下web.xml文件中常用的標籤以及功能
①:welcome-file-list
<welcome-file-list>
<welcome-file> index.jsp</welcome-file>
<welcome-file>index2.jsp</welcome-file>
PS:指定了2個歡迎頁面,顯示時按順序從第一個找起,如果第一個存在,就顯示第一個,後面的不起作用。如果第一個不存在,就找第二個,以此類推。
關於歡迎頁面:
訪問一個網站時,默認看到的第一個頁面就叫歡迎頁,一般情況下是由首頁來充當歡迎頁的。一般情況下,我們會在web.xml中指定歡迎頁。但 web.xml並不是一個Web的必要文件,沒有web.xml,網站仍然是可以正常工作的。只不過網站的功能複雜起來後,web.xml的確有非常大用處,所以,默認創建的動態web工程在WEB-INF文件夾下面都有一個web.xml文件。
②:servlet(下面是使用struts1是配置的servlet,並且看得出這裏的ACtionServlet其實是sturts1的核心)
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
③:servlet-mapping(這個也是對應到上面的servlet的servlet-mapping)
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
經過上面的配置,在servlet中能夠調用getServletConfig().getInitParameter("config")獲得參數名對應的值。
4、指定錯誤處理頁面,可以通過“異常類型”或“錯誤碼”來指定錯誤處理頁面。
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
-----------------------------
<error-page>
<exception-type>java.lang.Exception<exception-type>
<location>/exception.jsp<location>
</error-page>
⑤:filter(下面是一個編碼過濾器)
<filter>
<filter-name>XXXCharaSetFilter</filter-name>
<filter-class>net.test.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XXXCharaSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
看一看struts2的核心就是一個filter
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
⑥:監聽器listener,用web工程來實例化spring容器用的就是一個監聽器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ssh.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
⑦:設置會話(Session)過期時間,其中時間以分鐘爲單位,假如設置60分鐘超時:
<session-config>
<session-timeout>60</session-timeout>
</session-config>
web.xml配置詳解
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.