




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第詳細(xì)了解java監(jiān)聽器和過濾器目錄1、介紹:2、作用域?qū)ο螅篠ervt規(guī)范擴(kuò)展-----------過濾器接口1、介紹:2、具體作用:3、Filter接口實(shí)現(xiàn)類的開發(fā)步驟:三步第一步:創(chuàng)建一個(gè)java類實(shí)現(xiàn)Filter接口第二步:重寫doFilter接口中的doFilter()方法第三步:在web.xml文件中將過濾器接口實(shí)現(xiàn)類注冊到Http服務(wù)器OneServletTwoServlet4、Filter攔截地址的格式總結(jié)
1、介紹:
1)一組來自于Servlet規(guī)范下的接口,共有8個(gè)接口。在Tomcat中存在于Servlet-api.jar包
2)監(jiān)聽器接口需要由開發(fā)人員親自實(shí)現(xiàn),Http服務(wù)器提供的jar中并沒有對應(yīng)的實(shí)現(xiàn)類
3)監(jiān)聽器接口用于監(jiān)控【作用域?qū)ο笊芷诘淖兓瘯r(shí)刻】以及【作用域?qū)ο蠊蚕頂?shù)據(jù)的變化時(shí)刻】
2、作用域?qū)ο螅?/p>
1)在Servlet規(guī)范中,認(rèn)為在服務(wù)端內(nèi)存中可以在某些條件下為兩個(gè)Servlet之間提供數(shù)據(jù)共享方案的對象,被稱為【作用域?qū)ο蟆?/p>
2)在Servlet規(guī)范下的作用域?qū)ο螅?/p>
ServletContext:全局作用域?qū)ο?/p>
HttpSession:會(huì)話作用域?qū)ο?/p>
HttpServletRequest:請求作用域?qū)ο?/p>
3、監(jiān)聽器接口實(shí)現(xiàn)類開發(fā)規(guī)范:三步
1)根據(jù)監(jiān)聽的實(shí)際情況,選擇對應(yīng)的監(jiān)聽器接口進(jìn)行實(shí)現(xiàn)
2)重寫監(jiān)聽器接口中聲明的【監(jiān)聽事件處理方法】
3)在web.xml文件中將監(jiān)聽器接口實(shí)現(xiàn)類注冊到Http服務(wù)器中
4、ServletContextListener
1)作用:通過這個(gè)接口合法的檢測全局作用域?qū)ο蟮膬蓚€(gè)時(shí)刻
被初始化時(shí)刻被銷毀時(shí)刻
2)監(jiān)聽事件處理方法
publicvoidcontextInitialized():在全局作用域?qū)ο蟊籋ttp服務(wù)器初始化是調(diào)用
publicvoidcontextDestroyed():在全局作用域?qū)ο蟊籋ttp服務(wù)器銷毀時(shí)調(diào)用
5、ServletContextAttributeListener接口:
1)作用:通過這個(gè)接口合法的檢測全局作用域?qū)ο蠊蚕頂?shù)據(jù)變化的時(shí)刻
2)監(jiān)聽事件處理方法:
publicvoidcontextAdded():在全局作用域?qū)ο筇砑庸蚕頂?shù)據(jù)時(shí)調(diào)用
publicvoidcontextReplaced():在全局作用域?qū)ο蟾鹿蚕頂?shù)據(jù)時(shí)調(diào)用
publicvoidcontextRemoved():在全局作用域?qū)ο髣h除共享數(shù)據(jù)時(shí)調(diào)用
6、全局作用域?qū)ο蠊蚕頂?shù)據(jù)變化時(shí)刻
ServletContextapplication=request.getServletContext();
application.setAttribute("key1",100);//新增共享數(shù)據(jù)
application.setAttribute("key1",200);//更新共享數(shù)據(jù)
application.removeAttribute("key1");//刪除共享數(shù)據(jù)
代碼實(shí)現(xiàn)
以下就以ServletContextListener接口和ServletContextAttributeListener接口
第一步:選擇ServletContextListener接口進(jìn)行實(shí)現(xiàn)
第二步:重寫監(jiān)聽器接口聲明的【監(jiān)聽事件處理方法】
publicclassOneListenerimplementsServletContextListener{
@Override
publicvoidcontextInitialized(ServletContextEventsce){
System.out.println("Initialized............");
@Override
publicvoidcontextDestroyed(ServletContextEventsce){
System.out.println("Destroyed.............");
第三步:在web.xml中將監(jiān)聽器接口實(shí)現(xiàn)類注冊到Http服務(wù)器中
listener
listener-classschool.xauat.listener.OneListener/listener-class
/listener
由于ServletContext【全局作用對象的生命周期】貫穿網(wǎng)站的整個(gè)運(yùn)行期間
Servlet之間數(shù)據(jù)共享中有具體的ServletContext生命周期
因此在Tomcat服務(wù)器啟動(dòng)過程時(shí),執(zhí)行contextInitialize()方法
Initialized............
在Tomcat服務(wù)器準(zhǔn)備關(guān)閉時(shí),執(zhí)行contextDestroyed()方法
Destroyed.............
第一步:選擇ServletContextAttributeListener接口進(jìn)行實(shí)現(xiàn)
第二步:重寫監(jiān)聽器接口聲明的【監(jiān)聽事件處理方法】
publicclassOneListenerimplementsServletContextAttributeListener{
@Override
publicvoidattributeAdded(ServletContextAttributeEventscae){
System.out.println("ServletContextAttributeisadded......");
@Override
publicvoidattributeRemoved(ServletContextAttributeEventscae){
System.out.println("ServletContextAttributeisremoved......");
@Override
publicvoidattributeReplaced(ServletContextAttributeEventscae){
System.out.println("ServletContextAttributeisreplaced......");
第三步:在web.xml文件中將監(jiān)聽器接口實(shí)現(xiàn)類注冊到Tomcat服務(wù)器中
servlet
servlet-nameOneServlet/servlet-name
servlet-classschool.xauat.controller.OneServlet/servlet-class
/servlet
servlet-mapping
servlet-nameOneServlet/servlet-name
url-pattern/one/url-pattern
/servlet-mapping
listener
listener-classschool.xauat.listener.OneListener/listener-class
/listener
監(jiān)聽事件
publicclassOneServletextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
//通過請求對象獲取全局作用域?qū)ο?/p>
ServletContextapplication=request.getServletContext();
//向全局作用域?qū)ο笾刑砑庸蚕頂?shù)據(jù)
application.setAttribute("key",100);
//更改全局作用域?qū)ο笾械墓蚕頂?shù)據(jù)
application.setAttribute("key",500);
//刪除全局作用域?qū)ο笾械墓蚕頂?shù)據(jù)
application.removeAttribute("key");
運(yùn)行結(jié)果
Servt規(guī)范擴(kuò)展-----------過濾器接口
1、介紹:
1)來自于Servlet規(guī)范下的接口,在Tomcat中存在于servlet-api.jar包中
2)Filter接口實(shí)現(xiàn)類由開發(fā)人員負(fù)責(zé)提供的,Http服務(wù)器不負(fù)責(zé)提供
3)Filter接口會(huì)在Http服務(wù)器調(diào)用資源文件之前,對Http服務(wù)器進(jìn)行攔截
2、具體作用:
1)攔截Http服務(wù)器,幫助Http服務(wù)器去檢測當(dāng)前請求的合法性
2)攔截Http服務(wù)器,對當(dāng)前請求進(jìn)行增強(qiáng)操作
3、Filter接口實(shí)現(xiàn)類的開發(fā)步驟:三步
1)創(chuàng)建一個(gè)java類實(shí)現(xiàn)Filter接口
2)重寫Filter接口中的doFilter方法
3)在web.xml文件中將過濾器接口實(shí)現(xiàn)類注冊到Http服務(wù)器
過濾器檢測請求合法性
第一步:創(chuàng)建一個(gè)java類實(shí)現(xiàn)Filter接口
第二步:重寫doFilter接口中的doFilter()方法
*http://localhost:8080/myWeb/mm.jpgage=89
publicclassOneFilterimplementsFilter{
@Override
publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{
//通過攔截的請求對象來得到請求包中的參數(shù)信息,從而得到來訪用戶的真實(shí)年齡
Stringage=servletRequest.getParameter("age");
//根據(jù)這個(gè)年齡幫助我們的Http服務(wù)器判斷本次請求的合法性
if(Integer.valueOf(age)70){
//將攔截請求對象和相應(yīng)對象交換給Tomcat,由Tomcat繼續(xù)調(diào)用資源文件
filterChain.doFilter(servletRequest,servletResponse);
}else{
//過濾器代替Http服務(wù)器拒絕本次請求
servletResponse.setContentType("text/html;charset=utf-8");
PrintWriterout=servletResponse.getWriter();
out.print("centerfont不合適!?。?!/font/center
第三步:在web.xml文件中將過濾器接口實(shí)現(xiàn)類注冊到Http服務(wù)器
!--將過濾器類文件交給Tomcat--
filter
filter-nameOneFilter/filter-name
filter-classschool.xauat.filter.OneFilter/filter-class
/filter
!--通知Tomcat在調(diào)用何種資源文件是需要被當(dāng)前過濾器攔截--
filter-mapping
filter-nameOneFilter/filter-name
url-pattern/mm.jpg/url-pattern
/filter-mapping
過濾器對請求對象進(jìn)行增強(qiáng)服務(wù)
當(dāng)有多個(gè)以post的請求訪問服務(wù)器時(shí),需要對每個(gè)Servlet接口實(shí)現(xiàn)類中doPost()方法進(jìn)行以下操作,增加的開發(fā)的難度。
response.setCharacterEncoding("utf-8")
以下展示過濾器的作用:
第一步:創(chuàng)建java實(shí)現(xiàn)Filter接口
第二步:重寫Filter接口下的doFilter()方法
publicclassOneFilterimplementsFilter{
@Override
publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{
servletRequest.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
第三步:在web.xml文件中將過濾器接口實(shí)現(xiàn)類注冊到Http服務(wù)器
servlet
servlet-nameOneServlet/servlet-name
servlet-classschool.xauat.controller.OneServlet/servlet-class
/servlet
servlet-mapping
servlet-nameOneServlet/servlet-name
url-pattern/one/url-pattern
/servlet-mapping
servlet
servlet-nameTwoServlet/servlet-name
servlet-classschool.xauat.controller.TwoServlet/servlet-class
/servlet
servlet-mapping
servlet-nameTwoServlet/servlet-name
url-pattern/two/url-pattern
/servlet-mapping
!--注冊Filter類--
filter
filter-nameOneFilter/filter-name
filter-classschool.xauat.filter.OneFilter/filter-class
/filter
filter-mapping
filter-nameOneFilter/filter-name
!--通知Tomcat在調(diào)用所有資源文件之前都需要調(diào)用OneFilter進(jìn)行攔截--
url-pattern/*/url-pattern
/filter-mapping
OneServlet
publicclassOneServletextendsHttpServlet{
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
//通過請求對象獲取請求體中的請求參數(shù)
StringuserName=request.getParameter("userName");
System.out.println("OneServlet-----"+userName);
TwoServlet
publicclassTwoServletextendsHttpServlet{
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResp
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 新能源風(fēng)力發(fā)電標(biāo)準(zhǔn)必要專利許可與風(fēng)力發(fā)電場運(yùn)維合作協(xié)議
- 管理部部長工作概述
- 護(hù)理進(jìn)修述職
- 醫(yī)院培訓(xùn)課件:《康復(fù)醫(yī)學(xué)》
- 鋼架大棚安裝協(xié)議書
- 食堂轉(zhuǎn)讓轉(zhuǎn)租協(xié)議書
- 車商汽車合同協(xié)議書
- 餐廳股東解散協(xié)議書
- 酒后駕駛賠償協(xié)議書
- 車輛設(shè)備移交協(xié)議書
- 2025屆湖南省邵陽市高三下學(xué)期第三次聯(lián)考物理試卷(含答案)
- 2025年公證員資格考試全國范圍真題及答案
- 叉車作業(yè)安全協(xié)議書
- 房屋解除轉(zhuǎn)讓協(xié)議書
- 小學(xué)生美術(shù)講課課件
- 新聞采訪考試試題及答案
- DLT 593-2016 高壓開關(guān)設(shè)備和控制設(shè)備
- 高速鐵路路基聲屏障樁基試樁方案
- 手術(shù)質(zhì)量與安全分析報(bào)告模板
- 攪拌機(jī)課程設(shè)計(jì)
評論
0/150
提交評論