經過上一篇的介紹相信大家對JSP提供的過濾器一定有了一個概念,本篇我們就一起再來研究一下關于創建多個過濾器時,如果有兩個以上過濾器的過濾規則相同,那么這些過濾器的執行順序如何呢?答案是根據我們在web.xml中聲明的先后順序進行執行,也就是先聲明的先執行,后聲明的后執行。文字的描述大家可能還是不明白,下面就讓我們用程序驗證一下。
1、新建Filter類:
因為我們需要完成對于多個過濾器的,執行時的先后順序判斷,所以我們至少需要新建兩個Filter類。
a、firstFilter.java:
public class firstFilter implements Filter { public void destroy() { System.out.b、secondFilter.java:
public class secondFilter implements Filter { public void destroy() { System.out.println("Destroy----second"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("start----second"); chain.doFilter(request, response); System.out.println("end----second"); } public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Init----second"); }}2、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"> <!-- 過濾器對象firstFilter聲明 --> <filter> <filter-name>firstFilter</filter-name> <!-- 過濾器名 --> <filter-class>cn.imcook.filter.firstFilter</filter-class> <!-- 指定我們新建的過濾器對象的地址 --> </filter> <!-- 過濾器對象secondFilter聲明 --> <filter> <filter-name>secondFilter</filter-name> <filter-class>cn.imcook.filter.secondFilter</filter-class> </filter> <!-- 過濾器firstFilter的規則聲明 --> <filter-mapping> <filter-name>firstFilter</filter-name> <!-- 指定規則對于的過濾器對象 --> <url-pattern>/index.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> <!-- 該處有四個值可選,默認是REQUEST --> </filter-mapping> <!-- 過濾器secondFilter的規則聲明 --> <filter-mapping> <filter-name>secondFilter</filter-name> <url-pattern>/index.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <!-- 錯誤處理 --> <error-page> <error-code>404</error-code> <location>/error404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error500.jsp</location> </error-page> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config></web-app>3、啟動項目測試:
在瀏覽器地址欄輸入:http://localhost:8080/HelloWord/index.jsp,觀察myeclipse控制臺的輸出:
到這里我想大家對于多個Filter執行順序的問題,應該已經明白其中的道理了吧。
4、404、500錯誤過濾:
大家在上面的web.xml中一定看到了,兩個關于404、500錯誤的過濾監聽聲明,這是如何實現的呢?我們只需要在我們的web.xml中對這兩個錯誤進行一下聲明,系統就會開始監聽,一旦出現錯誤,將會跳轉到我們實現設置好的錯誤提醒頁面。
a、error404.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>404</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/CSS" href="styles.css"> --> </head> <body> <center> <h1>您訪問的地址不存在。<a href="index.jsp" style="color: red">返回首頁</a></h1> </center> </body></html>b、error500.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>500</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <h1>頁面出錯了,程序猿正在努力修復中...<a href="index.jsp" style="color: red">返回首頁</a></h1> </center> </body></html>c、用于測試500錯誤的Test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>測試</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my one web!"> </head> <body> <center> <h1>500錯誤驗證頁面</h1> <%=2/0 %><!-- 0不能作為被除數 --> </center> </body></html>5、效果:
a、當我們在地址欄輸入一個不存在頁面時:
b、當我們在地址欄輸入http://localhost:8080/HelloWord/Test.jsp:
到這里對于JSP提供Filter類的構建就為大家總結完畢,對于這些功能具體使用,還需大家自己好好摸索。如有疑問,歡迎留言討論。
新聞熱點
疑難解答