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的常用元素:
<web-app>
<display-name></display-name> 定義了WEB應用的名字。
<description></description> 聲明WEB應用的描述資訊。
<context-param></context-param> context-param元素宣告應用範圍內的初始化參數。
<filter></filter> 過濾器元素將一個名字與一個實現javax.servlet.Filter介面的類相關聯。
<filter-mapping></filter-mapping> 一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。
<listener></listener>servlet API的版本2.3增加了對事件監聽程式的支援,事件監聽程式在建立、修改和刪除會話或servlet環境時得到通知。 Listener元素指出事件監聽程式類。
<servlet></servlet> 在向servlet或JSP頁面制定初始化參數或定製URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務的。
<servlet-mapping></servlet-mapping>
伺服器一般為servlet提供一個預設的URL:http://host/webAppPrefix/servlet/ServletName。 但是,常常會更改這個URL,以便servlet可以訪問初始化參數或更容易地處理相對URL。在更改預設URL時,使用servlet-mapping元素。
<session-config></session-config> 如果某個會話在一定時間內未被訪問,伺服器可以拋棄它以節省記憶體。
可通過使用HttpSession的setMaxInactiveInterval方法明確設定單個會話對象的逾時值,或者可利用session-config元素制定預設逾時值。
<mime-mapping></mime-mapping>如果Web應用具有想到特殊的檔案,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。
<welcome-file-list></welcome-file-list>
指示伺服器在收到引用一個目錄名而不是檔案名稱的URL時,使用哪個檔案。
<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-app>
相應元素配置:
1.上下文參數:聲明應用範圍內的初始化參數。
<context-param>
<param-name>ContextParameter</para-name>
<param-value>test</param-value>
<description>It is a test
parameter.</description>
</context-param>
此所設定的參數,在JSP網頁中可以使用下列方法來取得:
${initParam.ContextParameter}
若在Servlet可以使用下列方法來獲得:
String param_name=getServletContext().getInitParamter("ContextParameter");
2.過濾器配置:將一個名字與一個實現javaxs.servlet.Filter介面的類相關聯。
<filter>
<filter-name>setCharacterEncoding</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>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
下面是使用Filter技術防止使用者非法訪問頁面的代碼:
1 <!-- 使用Filter技術防止使用者非法訪問頁面 -->2 <filter>3 <filter-name>isLogin</filter-name>4 <filter-class>org.seiosoft.framework.modules.util.constant.IsLoginFilter</filter-class>5 </filter>6 <filter-mapping>7 <filter-name>isLogin</filter-name>8 <url-pattern>/backstageManagement/*</url-pattern>9 </filter-mapping>
View Code
1 package org.seiosoft.framework.modules.util.constant; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest;10 import javax.servlet.ServletResponse;11 import javax.servlet.http.HttpServletRequest;12 import javax.servlet.http.HttpSession;13 14 import org.seiosoft.framework.modules.carrental.model.User;15 16 public class IsLoginFilter implements Filter {17 18 @Override19 public void destroy() {20 21 }22 23 @Override24 public void doFilter(ServletRequest request, ServletResponse response,25 FilterChain filterChain) throws IOException, ServletException {26 HttpServletRequest hrequest = (HttpServletRequest) request;27 HttpSession session = hrequest.getSession();28 User user = (User) session.getAttribute("managerUser");29 if (user == null) {30 System.out.println("user:"+user);31 hrequest.getRequestDispatcher("/backstageManagement/backLogin.jsp").forward(request,32 response);33 }34 filterChain.doFilter(request, response);35 }36 37 @Override38 public void init(FilterConfig arg0) throws ServletException {39 40 }41 42 }
View Code
3.監聽器配置
<listener>
<listerner-class>listener.SessionListener</listener-class>
</listener>
4.Servlet配置
基本配置
<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>snoop</servlet-name>
<url-pattern>/snoop</url-pattern>
</servlet-mapping>
5.會話逾時配置(單位為分鐘)
<session-config>
<session-timeout>120</session-timeout>
</session-config>
6.指定歡迎檔案頁配置
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
7.配置錯誤頁面
一、 通過錯誤碼來配置error-page
<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>
上面配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp。
二、通過異常的類型配置error-page
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
上面配置了當系統發生java.lang.NullException(即null 指標異常)時,跳轉到錯誤處理頁面error.jsp
8.struts配置
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,resources/struts/struts.xml</param-value>
</init-param>
<init-param>
<param-name>actionPackages</param-name>
<param-value>org.seiosoft.framework.modules</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>