詳細(xì)了解java監(jiān)聽(tīng)器和過(guò)濾器_第1頁(yè)
詳細(xì)了解java監(jiān)聽(tīng)器和過(guò)濾器_第2頁(yè)
詳細(xì)了解java監(jiān)聽(tīng)器和過(guò)濾器_第3頁(yè)
詳細(xì)了解java監(jiān)聽(tīng)器和過(guò)濾器_第4頁(yè)
詳細(xì)了解java監(jiān)聽(tīng)器和過(guò)濾器_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第詳細(xì)了解java監(jiān)聽(tīng)器和過(guò)濾器目錄1、介紹:2、作用域?qū)ο螅篠ervt規(guī)范擴(kuò)展-----------過(guò)濾器接口1、介紹:2、具體作用:3、Filter接口實(shí)現(xiàn)類(lèi)的開(kāi)發(fā)步驟:三步第一步:創(chuàng)建一個(gè)java類(lèi)實(shí)現(xiàn)Filter接口第二步:重寫(xiě)doFilter接口中的doFilter()方法第三步:在web.xml文件中將過(guò)濾器接口實(shí)現(xiàn)類(lèi)注冊(cè)到Http服務(wù)器OneServletTwoServlet4、Filter攔截地址的格式總結(jié)

1、介紹:

1)一組來(lái)自于Servlet規(guī)范下的接口,共有8個(gè)接口。在Tomcat中存在于Servlet-api.jar包

2)監(jiān)聽(tīng)器接口需要由開(kāi)發(fā)人員親自實(shí)現(xiàn),Http服務(wù)器提供的jar中并沒(méi)有對(duì)應(yīng)的實(shí)現(xiàn)類(lèi)

3)監(jiān)聽(tīng)器接口用于監(jiān)控【作用域?qū)ο笊芷诘淖兓瘯r(shí)刻】以及【作用域?qū)ο蠊蚕頂?shù)據(jù)的變化時(shí)刻】

2、作用域?qū)ο螅?/p>

1)在Servlet規(guī)范中,認(rèn)為在服務(wù)端內(nèi)存中可以在某些條件下為兩個(gè)Servlet之間提供數(shù)據(jù)共享方案的對(duì)象,被稱為【作用域?qū)ο蟆?/p>

2)在Servlet規(guī)范下的作用域?qū)ο螅?/p>

ServletContext:全局作用域?qū)ο?/p>

HttpSession:會(huì)話作用域?qū)ο?/p>

HttpServletRequest:請(qǐng)求作用域?qū)ο?/p>

3、監(jiān)聽(tīng)器接口實(shí)現(xiàn)類(lèi)開(kāi)發(fā)規(guī)范:三步

1)根據(jù)監(jiān)聽(tīng)的實(shí)際情況,選擇對(duì)應(yīng)的監(jiān)聽(tīng)器接口進(jìn)行實(shí)現(xiàn)

2)重寫(xiě)監(jiān)聽(tīng)器接口中聲明的【監(jiān)聽(tīng)事件處理方法】

3)在web.xml文件中將監(jiān)聽(tīng)器接口實(shí)現(xiàn)類(lèi)注冊(cè)到Http服務(wù)器中

4、ServletContextListener

1)作用:通過(guò)這個(gè)接口合法的檢測(cè)全局作用域?qū)ο蟮膬蓚€(gè)時(shí)刻

被初始化時(shí)刻被銷(xiāo)毀時(shí)刻

2)監(jiān)聽(tīng)事件處理方法

publicvoidcontextInitialized():在全局作用域?qū)ο蟊籋ttp服務(wù)器初始化是調(diào)用

publicvoidcontextDestroyed():在全局作用域?qū)ο蟊籋ttp服務(wù)器銷(xiāo)毀時(shí)調(diào)用

5、ServletContextAttributeListener接口:

1)作用:通過(guò)這個(gè)接口合法的檢測(cè)全局作用域?qū)ο蠊蚕頂?shù)據(jù)變化的時(shí)刻

2)監(jiān)聽(tīng)事件處理方法:

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)

第二步:重寫(xiě)監(jiān)聽(tīng)器接口聲明的【監(jiān)聽(tīng)事件處理方法】

publicclassOneListenerimplementsServletContextListener{

@Override

publicvoidcontextInitialized(ServletContextEventsce){

System.out.println("Initialized............");

@Override

publicvoidcontextDestroyed(ServletContextEventsce){

System.out.println("Destroyed.............");

第三步:在web.xml中將監(jiān)聽(tīng)器接口實(shí)現(xiàn)類(lèi)注冊(cè)到Http服務(wù)器中

listener

listener-classschool.xauat.listener.OneListener/listener-class

/listener

由于ServletContext【全局作用對(duì)象的生命周期】貫穿網(wǎng)站的整個(gè)運(yùn)行期間

Servlet之間數(shù)據(jù)共享中有具體的ServletContext生命周期

因此在Tomcat服務(wù)器啟動(dòng)過(guò)程時(shí),執(zhí)行contextInitialize()方法

Initialized............

在Tomcat服務(wù)器準(zhǔn)備關(guān)閉時(shí),執(zhí)行contextDestroyed()方法

Destroyed.............

第一步:選擇ServletContextAttributeListener接口進(jìn)行實(shí)現(xiàn)

第二步:重寫(xiě)監(jiān)聽(tīng)器接口聲明的【監(jiān)聽(tīng)事件處理方法】

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)聽(tīng)器接口實(shí)現(xiàn)類(lèi)注冊(cè)到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)聽(tīng)事件

publicclassOneServletextendsHttpServlet{

protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

//通過(guò)請(qǐng)求對(duì)象獲取全局作用域?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ò)展-----------過(guò)濾器接口

1、介紹:

1)來(lái)自于Servlet規(guī)范下的接口,在Tomcat中存在于servlet-api.jar包中

2)Filter接口實(shí)現(xiàn)類(lèi)由開(kāi)發(fā)人員負(fù)責(zé)提供的,Http服務(wù)器不負(fù)責(zé)提供

3)Filter接口會(huì)在Http服務(wù)器調(diào)用資源文件之前,對(duì)Http服務(wù)器進(jìn)行攔截

2、具體作用:

1)攔截Http服務(wù)器,幫助Http服務(wù)器去檢測(cè)當(dāng)前請(qǐng)求的合法性

2)攔截Http服務(wù)器,對(duì)當(dāng)前請(qǐng)求進(jìn)行增強(qiáng)操作

3、Filter接口實(shí)現(xiàn)類(lèi)的開(kāi)發(fā)步驟:三步

1)創(chuàng)建一個(gè)java類(lèi)實(shí)現(xiàn)Filter接口

2)重寫(xiě)Filter接口中的doFilter方法

3)在web.xml文件中將過(guò)濾器接口實(shí)現(xiàn)類(lèi)注冊(cè)到Http服務(wù)器

過(guò)濾器檢測(cè)請(qǐng)求合法性

第一步:創(chuàng)建一個(gè)java類(lèi)實(shí)現(xiàn)Filter接口

第二步:重寫(xiě)doFilter接口中的doFilter()方法

*http://localhost:8080/myWeb/mm.jpgage=89

publicclassOneFilterimplementsFilter{

@Override

publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{

//通過(guò)攔截的請(qǐng)求對(duì)象來(lái)得到請(qǐng)求包中的參數(shù)信息,從而得到來(lái)訪用戶的真實(shí)年齡

Stringage=servletRequest.getParameter("age");

//根據(jù)這個(gè)年齡幫助我們的Http服務(wù)器判斷本次請(qǐng)求的合法性

if(Integer.valueOf(age)70){

//將攔截請(qǐng)求對(duì)象和相應(yīng)對(duì)象交換給Tomcat,由Tomcat繼續(xù)調(diào)用資源文件

filterChain.doFilter(servletRequest,servletResponse);

}else{

//過(guò)濾器代替Http服務(wù)器拒絕本次請(qǐng)求

servletResponse.setContentType("text/html;charset=utf-8");

PrintWriterout=servletResponse.getWriter();

out.print("centerfont不合適?。。。?font/center

第三步:在web.xml文件中將過(guò)濾器接口實(shí)現(xiàn)類(lèi)注冊(cè)到Http服務(wù)器

!--將過(guò)濾器類(lèi)文件交給Tomcat--

filter

filter-nameOneFilter/filter-name

filter-classschool.xauat.filter.OneFilter/filter-class

/filter

!--通知Tomcat在調(diào)用何種資源文件是需要被當(dāng)前過(guò)濾器攔截--

filter-mapping

filter-nameOneFilter/filter-name

url-pattern/mm.jpg/url-pattern

/filter-mapping

過(guò)濾器對(duì)請(qǐng)求對(duì)象進(jìn)行增強(qiáng)服務(wù)

當(dāng)有多個(gè)以post的請(qǐng)求訪問(wèn)服務(wù)器時(shí),需要對(duì)每個(gè)Servlet接口實(shí)現(xiàn)類(lèi)中doPost()方法進(jìn)行以下操作,增加的開(kāi)發(fā)的難度。

response.setCharacterEncoding("utf-8")

以下展示過(guò)濾器的作用:

第一步:創(chuàng)建java實(shí)現(xiàn)Filter接口

第二步:重寫(xiě)Filter接口下的doFilter()方法

publicclassOneFilterimplementsFilter{

@Override

publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{

servletRequest.setCharacterEncoding("utf-8");

filterChain.doFilter(servletRequest,servletResponse);

第三步:在web.xml文件中將過(guò)濾器接口實(shí)現(xiàn)類(lèi)注冊(cè)到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

!--注冊(cè)Filter類(lèi)--

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{

//通過(guò)請(qǐng)求對(duì)象獲取請(qǐng)求體中的請(qǐng)求參數(shù)

StringuserName=request.getParameter("userName");

System.out.println("OneServlet-----"+userName);

TwoServlet

publicclassTwoServletextendsHttpServlet{

protectedvoiddoPost(HttpServletRequestrequest,HttpServletResp

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論