日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Servlet过滤器介绍之原理分析(转)

發(fā)布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Servlet过滤器介绍之原理分析(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://zhangjunhd.blog.51cto.com/113473/20629 本文主要介紹Servlet過濾器的基本原理 author: ZJ 2007-2-21 Blog: [url]http://zhangjunhd.blog.51cto.com/[/url] 1Servlet過濾器 1.1 什么是過濾器 過濾器是一個程序,它先于與之相關(guān)的servletJSP頁面運(yùn)行在服務(wù)器上。過濾器可附加到一個或多個servletJSP頁面上,并且可以檢查進(jìn)入這些資源的請求信息。在這之后,過濾器可以作如下的選擇: ①以常規(guī)的方式調(diào)用資源(即,調(diào)用servletJSP頁面)。 ②利用修改過的請求信息調(diào)用資源。 ③調(diào)用資源,但在發(fā)送響應(yīng)到客戶機(jī)前對其進(jìn)行修改。 ④阻止該資源調(diào)用,代之以轉(zhuǎn)到其他的資源,返回一個特定的狀態(tài)代碼或生成替換輸出。 1.2 Servlet過濾器的基本原理 Servlet作 為過濾器使用時,它可以對客戶的請求進(jìn)行處理。處理完成后,它會交給下一個過濾器處理,這樣,客戶的請求在過濾鏈里逐個處理,直到請求發(fā)送到目標(biāo)為止。例 如,某網(wǎng)站里有提交“修改的注冊信息”的網(wǎng)頁,當(dāng)用戶填寫完修改信息并提交后,服務(wù)器在進(jìn)行處理時需要做兩項工作:判斷客戶端的會話是否有效;對提交的數(shù) 據(jù)進(jìn)行統(tǒng)一編碼。這兩項工作可以在由兩個過濾器組成的過濾鏈里進(jìn)行處理。當(dāng)過濾器處理成功后,把提交的數(shù)據(jù)發(fā)送到最終目標(biāo);如果過濾器處理不成功,將把視 圖派發(fā)到指定的錯誤頁面。 2Servlet過濾器開發(fā)步驟 開發(fā)Servlet過濾器的步驟如下: ①編寫實現(xiàn)Filter接口的Servlet類。 ②在web.xml中配置Filter 開發(fā)一個過濾器需要實現(xiàn)Filter接口,Filter接口定義了以下方法: destory()由Web容器調(diào)用,初始化此Filter initFilterConfig filterConfig)由Web容器調(diào)用,初始化此Filter doFilterServletRequest request,ServletResponse response,FilterChain chain)具體過濾處理代碼。 3.一個過濾器框架實例 SimpleFilter1.java
package com.zj.sample; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; ? public class SimpleFilter1 implements Filter { ??? @SuppressWarnings("unused") ??? private FilterConfig filterConfig; ? ??? public void init(FilterConfig config) throws ServletException { ?????? this.filterConfig = config; ??? } ? ??? public void doFilter(ServletRequest request, ServletResponse response, ?????????? FilterChain chain) { ?????? try { ?????????? System.out.println("Within SimpleFilter1:Filtering the Request..."); ?????????? chain.doFilter(request, response);// 把處理發(fā)送到下一個過濾器 ?????????? System.out .println("Within SimpleFilter1:Filtering the Response..."); ?????? } catch (IOException ioe) { ?????????? ioe.printStackTrace(); ?????? } catch (ServletException se) { ?????????? se.printStackTrace(); ?????? } ??? } ? ??? public void destroy() { ?????? this.filterConfig = null; ??? } }
SimpleFilter2.java
package com.zj.sample; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; ? public class SimpleFilter2 implements Filter { ??? @SuppressWarnings("unused") ??? private FilterConfig filterConfig; ? ??? public void init(FilterConfig config) throws ServletException { ?????? this.filterConfig = config; ??? } ? ??? public void doFilter(ServletRequest request, ServletResponse response, ?????????? FilterChain chain) { ?????? try { ?????????? System.out.println("Within SimpleFilter2:Filtering the Request..."); ?????????? chain.doFilter(request, response); // 把處理發(fā)送到下一個過濾器 ?????????? System.out.println("Within SimpleFilter2:Filtering the Response..."); ?????? } catch (IOException ioe) { ?????????? ioe.printStackTrace(); ?????? } catch (ServletException se) { ?????????? se.printStackTrace(); ?????? } ??? } ? ??? public void destroy() { ?????? this.filterConfig = null; ??? } }
web.xml
<filter> ??? <filter-name>filter1</filter-name> ??? <filter-class>com.zj.sample.SimpleFilter1</filter-class> </filter> <filter-mapping> ??? <filter-name>filter1</filter-name> ??? <url-pattern>/*</url-pattern>//為所有的訪問做過濾 </filter-mapping> ? <filter> ??? <filter-name>filter2</filter-name> ??? <filter-class>com.zj.sample.SimpleFilter2</filter-class> </filter> <filter-mapping> ??? <filter-name>filter2</filter-name> ??? <url-pattern>/*</url-pattern>//為所有的訪問做過濾 </filter-mapping>
打開web容器中任意頁面輸出結(jié)果:(注意過濾器執(zhí)行的請求/響應(yīng)順序) Within SimpleFilter1:Filtering the Request...
Within SimpleFilter2:Filtering the Request...
Within SimpleFilter2:Filtering the Response...
Within SimpleFilter1:Filtering the Response...
4.報告過濾器 我們來試驗一個簡單的過濾器,只要調(diào)用相關(guān)的servletJSP頁面,它就打印一條消息到標(biāo)準(zhǔn)輸出。為實現(xiàn)此功能,在doFilter方法中執(zhí)行過濾行為。每當(dāng)調(diào)用與這個過濾器相關(guān)的servletJSP頁面時,doFilter方法就生成一個打印輸出,此輸出列出請求主機(jī)和調(diào)用的URL。因為getRequestURL方法位于HttpServletRequest而不是ServletRequest中,所以把ServletRequest對象構(gòu)造為HttpServletRequest類型。我們改動一下章節(jié)3SimpleFilter1.java SimpleFilter1.java
package com.zj.sample; import java.io.IOException; import java.util.Date; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; ? public class SimpleFilter1 implements Filter { ??? @SuppressWarnings("unused") ??? private FilterConfig filterConfig; ? ??? public void init(FilterConfig config) throws ServletException { ?????? this.filterConfig = config; ??? } ? ??? public void doFilter(ServletRequest request, ServletResponse response, ?????????? FilterChain chain) { ?????? try { ?????????? System.out.println("Within SimpleFilter1:Filtering the Request..."); ?????????? HttpServletRequest req = (HttpServletRequest) request; ?????????? System.out.println(req.getRemoteHost() + " tried to access " ????????????????? + req.getRequestURL() + " on " + new Date() + "."); ?????????? chain.doFilter(request, response); ?????????? System.out.println("Within SimpleFilter1:Filtering the Response..."); ?????? } catch (IOException ioe) { ?????????? ioe.printStackTrace(); ?????? } catch (ServletException se) { ?????????? se.printStackTrace(); ?????? } ??? } ? ??? public void destroy() { ?????? this.filterConfig = null; ??? } }
web.xml設(shè)置不變,同章節(jié)3 測試: 輸入[url]http://localhost:8080/Test4Jsp/login.jsp[/url] 結(jié)果: Within SimpleFilter1:Filtering the Request...
0:0:0:0:0:0:0:1 tried to access [url]http://localhost:8080/Test4Jsp/login.jsp[/url] on Sun Mar 04 17:01:37 CST 2007.
Within SimpleFilter2:Filtering the Request...
Within SimpleFilter2:Filtering the Response...
Within SimpleFilter1:Filtering the Response...
5.訪問時的過濾器(在過濾器中使用servlet初始化參數(shù)) 下面利用init設(shè)定一個正常訪問時間范圍,對那些不在此時間段的訪問作出記錄。我們改動一下章節(jié)3SimpleFilter2.java SimpleFilter2.java
package com.zj.sample; import java.io.IOException; import java.text.DateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; ? public class SimpleFilter2 implements Filter { ??? @SuppressWarnings("unused") ??? private FilterConfig config; ??? private ServletContext context; ??? private int startTime, endTime; ??? private DateFormat formatter; ? ??? public void init(FilterConfig config) throws ServletException { ?????? this.config = config; ?????? context = config.getServletContext(); ?????? formatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, ????????????? DateFormat.MEDIUM); ?????? try { ?????????? startTime = Integer.parseInt(config.getInitParameter("startTime"));// web.xml ?????????? endTime = Integer.parseInt(config.getInitParameter("endTime"));// web.xml ?????? } catch (NumberFormatException nfe) { // Malformed or null ?????????? // Default: access at or after 10 p.m. but before 6 a.m. is ?????????? // considered unusual. ?????????? startTime = 22; // 10:00 p.m. ?????????? endTime = 6; // 6:00 a.m. ?????? } ??? } ? ??? public void doFilter(ServletRequest request, ServletResponse response, ?????????? FilterChain chain) { ?????? try { ?????????? System.out.println("Within SimpleFilter2:Filtering the Request..."); ?????????? HttpServletRequest req = (HttpServletRequest) request; ?????????? GregorianCalendar calendar = new GregorianCalendar(); ?????????? int currentTime = calendar.get(Calendar.HOUR_OF_DAY); ?????????? if (isUnusualTime(currentTime, startTime, endTime)) { ????????????? context.log("WARNING: " + req.getRemoteHost() + " accessed " ???????????????????? + req.getRequestURL() + " on " ???????????????????? + formatter.format(calendar.getTime())); ????????????? // The log file is under <CATALINA_HOME>/logs.One log per day. ?????????? } ?????????? chain.doFilter(request, response); ?????????? System.out ????????????????? .println("Within SimpleFilter2:Filtering the Response..."); ?????? } catch (IOException ioe) { ?????????? ioe.printStackTrace(); ?????? } catch (ServletException se) { ?????????? se.printStackTrace(); ?????? } ??? } ? ??? public void destroy() {} ? ??? // Is the current time between the start and end ??? // times that are marked as abnormal access times? ??? private boolean isUnusualTime(int currentTime, int startTime, int endTime) { ?????? // If the start time is less than the end time (i.e., ?????? // they are two times on the same day), then the ?????? // current time is considered unusual if it is ?????? // between the start and end times. ?????? if (startTime < endTime) { ?????????? return ((currentTime >= startTime) && (currentTime < endTime)); ?????? } ?????? // If the start time is greater than or equal to the ?????? // end time (i.e., the start time is on one day and ?????? // the end time is on the next day), then the current ?????? // time is considered unusual if it is NOT between ?????? // the end and start times. ?????? else { ?????????? return (!isUnusualTime(currentTime, endTime, startTime)); ?????? } ??? } }
web.xml設(shè)置不變。 關(guān)于Tomcat日志處理,這里補(bǔ)充介紹一下。config.getServletContext().log"log message")會將日志信息寫入<CATALINA_HOME>/logs文件夾下,文件名應(yīng)該為localhost_log.2007-03-04.txt這樣的形式(按日期每天產(chǎn)生一個,第二天可以看見)。要得到這樣一個日志文件,應(yīng)該在server.xml中有: <Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>

轉(zhuǎn)載于:https://my.oschina.net/liangzhenghui/blog/110469

總結(jié)

以上是生活随笔為你收集整理的Servlet过滤器介绍之原理分析(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。