詳細了解java監(jiān)聽器和過濾器_第1頁
詳細了解java監(jiān)聽器和過濾器_第2頁
詳細了解java監(jiān)聽器和過濾器_第3頁
詳細了解java監(jiān)聽器和過濾器_第4頁
詳細了解java監(jiān)聽器和過濾器_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

第詳細了解java監(jiān)聽器和過濾器目錄1、介紹:2、作用域對象:Servt規(guī)范擴展-----------過濾器接口1、介紹:2、具體作用:3、Filter接口實現(xiàn)類的開發(fā)步驟:三步第一步:創(chuàng)建一個java類實現(xiàn)Filter接口第二步:重寫doFilter接口中的doFilter()方法第三步:在web.xml文件中將過濾器接口實現(xiàn)類注冊到Http服務器OneServletTwoServlet4、Filter攔截地址的格式總結

1、介紹:

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

2)監(jiān)聽器接口需要由開發(fā)人員親自實現(xiàn),Http服務器提供的jar中并沒有對應的實現(xiàn)類

3)監(jiān)聽器接口用于監(jiān)控【作用域對象生命周期的變化時刻】以及【作用域對象共享數(shù)據(jù)的變化時刻】

2、作用域對象:

1)在Servlet規(guī)范中,認為在服務端內(nèi)存中可以在某些條件下為兩個Servlet之間提供數(shù)據(jù)共享方案的對象,被稱為【作用域對象】

2)在Servlet規(guī)范下的作用域對象:

ServletContext:全局作用域對象

HttpSession:會話作用域對象

HttpServletRequest:請求作用域對象

3、監(jiān)聽器接口實現(xiàn)類開發(fā)規(guī)范:三步

1)根據(jù)監(jiān)聽的實際情況,選擇對應的監(jiān)聽器接口進行實現(xiàn)

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

3)在web.xml文件中將監(jiān)聽器接口實現(xiàn)類注冊到Http服務器中

4、ServletContextListener

1)作用:通過這個接口合法的檢測全局作用域對象的兩個時刻

被初始化時刻被銷毀時刻

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

publicvoidcontextInitialized():在全局作用域對象被Http服務器初始化是調用

publicvoidcontextDestroyed():在全局作用域對象被Http服務器銷毀時調用

5、ServletContextAttributeListener接口:

1)作用:通過這個接口合法的檢測全局作用域對象共享數(shù)據(jù)變化的時刻

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

publicvoidcontextAdded():在全局作用域對象添加共享數(shù)據(jù)時調用

publicvoidcontextReplaced():在全局作用域對象更新共享數(shù)據(jù)時調用

publicvoidcontextRemoved():在全局作用域對象刪除共享數(shù)據(jù)時調用

6、全局作用域對象共享數(shù)據(jù)變化時刻

ServletContextapplication=request.getServletContext();

application.setAttribute("key1",100);//新增共享數(shù)據(jù)

application.setAttribute("key1",200);//更新共享數(shù)據(jù)

application.removeAttribute("key1");//刪除共享數(shù)據(jù)

代碼實現(xiàn)

以下就以ServletContextListener接口和ServletContextAttributeListener接口

第一步:選擇ServletContextListener接口進行實現(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)聽器接口實現(xiàn)類注冊到Http服務器中

listener

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

/listener

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

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

因此在Tomcat服務器啟動過程時,執(zhí)行contextInitialize()方法

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

在Tomcat服務器準備關閉時,執(zhí)行contextDestroyed()方法

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

第一步:選擇ServletContextAttributeListener接口進行實現(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)聽器接口實現(xiàn)類注冊到Tomcat服務器中

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{

//通過請求對象獲取全局作用域對象

ServletContextapplication=request.getServletContext();

//向全局作用域對象中添加共享數(shù)據(jù)

application.setAttribute("key",100);

//更改全局作用域對象中的共享數(shù)據(jù)

application.setAttribute("key",500);

//刪除全局作用域對象中的共享數(shù)據(jù)

application.removeAttribute("key");

運行結果

Servt規(guī)范擴展-----------過濾器接口

1、介紹:

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

2)Filter接口實現(xiàn)類由開發(fā)人員負責提供的,Http服務器不負責提供

3)Filter接口會在Http服務器調用資源文件之前,對Http服務器進行攔截

2、具體作用:

1)攔截Http服務器,幫助Http服務器去檢測當前請求的合法性

2)攔截Http服務器,對當前請求進行增強操作

3、Filter接口實現(xiàn)類的開發(fā)步驟:三步

1)創(chuàng)建一個java類實現(xiàn)Filter接口

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

3)在web.xml文件中將過濾器接口實現(xiàn)類注冊到Http服務器

過濾器檢測請求合法性

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

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

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

publicclassOneFilterimplementsFilter{

@Override

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

//通過攔截的請求對象來得到請求包中的參數(shù)信息,從而得到來訪用戶的真實年齡

Stringage=servletRequest.getParameter("age");

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

if(Integer.valueOf(age)70){

//將攔截請求對象和相應對象交換給Tomcat,由Tomcat繼續(xù)調用資源文件

filterChain.doFilter(servletRequest,servletResponse);

}else{

//過濾器代替Http服務器拒絕本次請求

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

PrintWriterout=servletResponse.getWriter();

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

第三步:在web.xml文件中將過濾器接口實現(xiàn)類注冊到Http服務器

!--將過濾器類文件交給Tomcat--

filter

filter-nameOneFilter/filter-name

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

/filter

!--通知Tomcat在調用何種資源文件是需要被當前過濾器攔截--

filter-mapping

filter-nameOneFilter/filter-name

url-pattern/mm.jpg/url-pattern

/filter-mapping

過濾器對請求對象進行增強服務

當有多個以post的請求訪問服務器時,需要對每個Servlet接口實現(xiàn)類中doPost()方法進行以下操作,增加的開發(fā)的難度。

response.setCharacterEncoding("utf-8")

以下展示過濾器的作用:

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

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

publicclassOneFilterimplementsFilter{

@Override

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

servletRequest.setCharacterEncoding("utf-8");

filterChain.doFilter(servletRequest,servletResponse);

第三步:在web.xml文件中將過濾器接口實現(xiàn)類注冊到Http服務器

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在調用所有資源文件之前都需要調用OneFilter進行攔截--

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)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論