經(jīng)過上一篇的介紹相信大家對(duì)JSP提供的過濾器一定有了一個(gè)概念,本篇我們就一起再來研究一下關(guān)于創(chuàng)建多個(gè)過濾器時(shí),如果有兩個(gè)以上過濾器的過濾規(guī)則相同,那么這些過濾器的執(zhí)行順序如何呢?答案是根據(jù)我們?cè)趙eb.xml中聲明的先后順序進(jìn)行執(zhí)行,也就是先聲明的先執(zhí)行,后聲明的后執(zhí)行。文字的描述大家可能還是不明白,下面就讓我們用程序驗(yàn)證一下。
1、新建Filter類:
因?yàn)槲覀冃枰瓿蓪?duì)于多個(gè)過濾器的,執(zhí)行時(shí)的先后順序判斷,所以我們至少需要新建兩個(gè)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聲明:
這里需要注意的時(shí),要達(dá)到上面的效果,我們需要在聲明過濾規(guī)則中,保證兩個(gè)過濾器匹配的請(qǐng)求一致。
<?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"> <!-- 過濾器對(duì)象firstFilter聲明 --> <filter> <filter-name>firstFilter</filter-name> <!-- 過濾器名 --> <filter-class>cn.imcook.filter.firstFilter</filter-class> <!-- 指定我們新建的過濾器對(duì)象的地址 --> </filter> <!-- 過濾器對(duì)象secondFilter聲明 --> <filter> <filter-name>secondFilter</filter-name> <filter-class>cn.imcook.filter.secondFilter</filter-class> </filter> <!-- 過濾器firstFilter的規(guī)則聲明 --> <filter-mapping> <filter-name>firstFilter</filter-name> <!-- 指定規(guī)則對(duì)于的過濾器對(duì)象 --> <url-pattern>/index.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> <!-- 該處有四個(gè)值可選,默認(rèn)是REQUEST --> </filter-mapping> <!-- 過濾器secondFilter的規(guī)則聲明 --> <filter-mapping> <filter-name>secondFilter</filter-name> <url-pattern>/index.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <!-- 錯(cuò)誤處理 --> <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、啟動(dòng)項(xiàng)目測(cè)試:
在瀏覽器地址欄輸入:http://localhost:8080/HelloWord/index.jsp,觀察myeclipse控制臺(tái)的輸出:

到這里我想大家對(duì)于多個(gè)Filter執(zhí)行順序的問題,應(yīng)該已經(jīng)明白其中的道理了吧。
4、404、500錯(cuò)誤過濾:
大家在上面的web.xml中一定看到了,兩個(gè)關(guān)于404、500錯(cuò)誤的過濾監(jiān)聽聲明,這是如何實(shí)現(xiàn)的呢?我們只需要在我們的web.xml中對(duì)這兩個(gè)錯(cuò)誤進(jìn)行一下聲明,系統(tǒng)就會(huì)開始監(jiān)聽,一旦出現(xiàn)錯(cuò)誤,將會(huì)跳轉(zhuǎn)到我們實(shí)現(xiàn)設(shè)置好的錯(cuò)誤提醒頁面。
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>頁面出錯(cuò)了,程序猿正在努力修復(fù)中...<a href="index.jsp" style="color: red">返回首頁</a></h1> </center> </body></html>
c、用于測(cè)試500錯(cuò)誤的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>測(cè)試</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錯(cuò)誤驗(yàn)證頁面</h1> <%=2/0 %><!-- 0不能作為被除數(shù) --> </center> </body></html>
5、效果:
a、當(dāng)我們?cè)诘刂窓谳斎胍粋€(gè)不存在頁面時(shí):

b、當(dāng)我們?cè)诘刂窓谳斎雋ttp://localhost:8080/HelloWord/Test.jsp:

到這里對(duì)于JSP提供Filter類的構(gòu)建就為大家總結(jié)完畢,對(duì)于這些功能具體使用,還需大家自己好好摸索。如有疑問,歡迎留言討論。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注