詳細了解java監(jiān)聽器和過濾器
1、介紹:
1)一組來自于Servlet規(guī)范下的接口,共有8個接口。在Tomcat中存在于Servlet-api.jar包
2)監(jiān)聽器接口需要由開發(fā)人員親自實現(xiàn),Http服務器提供的jar中并沒有對應的實現(xiàn)類
3)監(jiān)聽器接口用于監(jiān)控【作用域?qū)ο笊芷诘淖兓瘯r刻】以及【作用域?qū)ο蠊蚕頂?shù)據(jù)的變化時刻】
2、作用域?qū)ο螅?/h2>
1)在Servlet規(guī)范中,認為在服務端內(nèi)存中可以在某些條件下為兩個Servlet之間提供數(shù)據(jù)共享方案的對象,被稱為【作用域?qū)ο蟆?/p>
2)在Servlet規(guī)范下的作用域?qū)ο螅?/p>
ServletContext:全局作用域?qū)ο?/p>
HttpSession:會話作用域?qū)ο?/p>
HttpServletRequest:請求作用域?qū)ο?/p>
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)作用:通過這個接口合法的檢測全局作用域?qū)ο蟮膬蓚€時刻
被初始化時刻 被銷毀時刻
2)監(jiān)聽事件處理方法
public void contextInitialized():在全局作用域?qū)ο蟊籋ttp服務器初始化是調(diào)用
public void contextDestroyed():在全局作用域?qū)ο蟊籋ttp服務器銷毀時調(diào)用
5、ServletContextAttributeListener接口:
1)作用:通過這個接口合法的檢測全局作用域?qū)ο蠊蚕頂?shù)據(jù)變化的時刻
2)監(jiān)聽事件處理方法:
public void contextAdded():在全局作用域?qū)ο筇砑庸蚕頂?shù)據(jù)時調(diào)用
public void contextReplaced():在全局作用域?qū)ο蟾鹿蚕頂?shù)據(jù)時調(diào)用
public void contextRemoved():在全局作用域?qū)ο髣h除共享數(shù)據(jù)時調(diào)用
6、全局作用域?qū)ο蠊蚕頂?shù)據(jù)變化時刻
ServletContext application=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)聽事件處理方法】
public class OneListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Initialized............");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Destroyed.............");
}
}
第三步:在web.xml中將監(jiān)聽器接口實現(xiàn)類注冊到Http服務器中
<listener>
<listener-class>school.xauat.listener.OneListener</listener-class>
</listener>
由于ServletContext【全局作用對象的生命周期】貫穿網(wǎng)站的整個運行期間
Servlet之間數(shù)據(jù)共享中有具體的ServletContext生命周期
因此在Tomcat服務器啟動過程時,執(zhí)行contextInitialize()方法
Initialized............
在Tomcat服務器準備關(guān)閉時,執(zhí)行contextDestroyed()方法
Destroyed.............
第一步:選擇ServletContextAttributeListener接口進行實現(xiàn)
第二步:重寫監(jiān)聽器接口聲明的【監(jiān)聽事件處理方法】
public class OneListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scae) {
System.out.println("ServletContextAttribute is added......");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae){
System.out.println("ServletContextAttribute is removed......");
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae){
System.out.println("ServletContextAttribute is replaced......");
}
}
第三步:在web.xml文件中將監(jiān)聽器接口實現(xiàn)類注冊到Tomcat服務器中
<servlet>
<servlet-name>OneServlet</servlet-name>
<servlet-class>school.xauat.controller.OneServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OneServlet</servlet-name>
<url-pattern>/one</url-pattern>
</servlet-mapping>
<listener>
<listener-class>school.xauat.listener.OneListener</listener-class>
</listener>
監(jiān)聽事件
public class OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通過請求對象獲取全局作用域?qū)ο?
ServletContext application=request.getServletContext();
//向全局作用域?qū)ο笾刑砑庸蚕頂?shù)據(jù)
application.setAttribute("key",100);
//更改全局作用域?qū)ο笾械墓蚕頂?shù)據(jù)
application.setAttribute("key",500);
//刪除全局作用域?qū)ο笾械墓蚕頂?shù)據(jù)
application.removeAttribute("key");
}
}
運行結(jié)果

Servt規(guī)范擴展-----------過濾器接口
1、介紹:
1)來自于Servlet規(guī)范下的接口,在Tomcat中存在于servlet-api.jar包中
2)Filter接口實現(xiàn)類由開發(fā)人員負責提供的,Http服務器不負責提供
3)Filter接口會在Http服務器調(diào)用資源文件之前,對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.jpg?age=89
*/
public class OneFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//通過攔截的請求對象來得到請求包中的參數(shù)信息,從而得到來訪用戶的真實年齡
String age=servletRequest.getParameter("age");
//根據(jù)這個年齡幫助我們的Http服務器判斷本次請求的合法性
if(Integer.valueOf(age)<70){
//將攔截請求對象和相應對象交換給Tomcat,由Tomcat繼續(xù)調(diào)用資源文件
filterChain.doFilter(servletRequest,servletResponse);
}else{
//過濾器代替Http服務器拒絕本次請求
servletResponse.setContentType("text/html;charset=utf-8");
PrintWriter out=servletResponse.getWriter();
out.print("<center><font style='color:red;font-size:40px'>不合適?。。?!</font></center>");
}
}
}
第三步:在web.xml文件中將過濾器接口實現(xiàn)類注冊到Http服務器
<!--將過濾器類文件交給Tomcat-->
<filter>
<filter-name>OneFilter</filter-name>
<filter-class>school.xauat.filter.OneFilter</filter-class>
</filter>
<!--通知Tomcat在調(diào)用何種資源文件是需要被當前過濾器攔截-->
<filter-mapping>
<filter-name>OneFilter</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()方法
public class OneFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
}
第三步:在web.xml文件中將過濾器接口實現(xiàn)類注冊到Http服務器
<servlet>
<servlet-name>OneServlet</servlet-name>
<servlet-class>school.xauat.controller.OneServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OneServlet</servlet-name>
<url-pattern>/one</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>TwoServlet</servlet-name>
<servlet-class>school.xauat.controller.TwoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TwoServlet</servlet-name>
<url-pattern>/two</url-pattern>
</servlet-mapping>
<!--注冊Filter類-->
<filter>
<filter-name>OneFilter</filter-name>
<filter-class>school.xauat.filter.OneFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OneFilter</filter-name>
<!--通知Tomcat在調(diào)用所有資源文件之前都需要調(diào)用OneFilter進行攔截-->
<url-pattern>/*</url-pattern>
</filter-mapping>
OneServlet
public class OneServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通過請求對象獲取請求體中的請求參數(shù)
String userName=request.getParameter("userName");
System.out.println("OneServlet----->"+userName);
}
}
TwoServlet
public class TwoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通過請求對象獲取請求體中的請求參數(shù)
String userName=request.getParameter("userName");
System.out.println("TwoServlet---->"+userName);
}
}
4、Filter攔截地址的格式
1)命令格式:
<filter-mapping>
<filter-name><OneFilter/filter-name>
<url-pattern>攔截地址</url-pattern>
</filter-mapping>
2)命令作用:
攔截地址通知Tomcat在調(diào)用和何種資源文件之前需要調(diào)用OneFilter過濾進行攔截
3)要求Tomcat在調(diào)用某一個具體文件之前,來調(diào)用OneFilter進行攔截
<url-pattern>/目錄/文件名</url-pattern>
4)要求Tomcat在調(diào)用某一個文件夾下所有的資源文件之前,來調(diào)用OneFilter進行攔截
<url-pattern>/目錄/*</url-pattern>
5)要求Tomcat在調(diào)用任意文件夾下的某種類型文件之前,來調(diào)用OneFilter攔截
<url-pattern>*.jpg</url-pattern>
6)要求Tomcat在調(diào)用網(wǎng)站中任意文件時,來調(diào)用OneFilter攔截
<url-pattern>/*</url-pattern>
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望你能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
SpringBoot集成SpirePDF實現(xiàn)文本替換功能
SpirePDF是一個用于.NET平臺的高級PDF文檔處理庫,它提供了一套完整的API,允許開發(fā)者創(chuàng)建、編輯、轉(zhuǎn)換、合并、分割和解析PDF文件本文給大家介紹了SpringBoot集成SpirePDF實現(xiàn)文本替換功能,需要的朋友可以參考下2024-09-09
java 實現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值
今天小編就為大家分享一篇java 實現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Java語言實現(xiàn)Blowfish加密算法完整代碼分享
這篇文章主要介紹了Java語言實現(xiàn)Blowfish加密算法完整代碼分享,簡單介紹了blowfish加密算法,具有一定借鑒價值,需要的朋友可以參考下。2017-11-11
Spring Boot 與 Kotlin 上傳文件的示例代碼
這篇文章主要介紹了Spring Boot 與 Kotlin 上傳文件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
SpringBoot ResponseEntity標識Http響應方式
這篇文章主要介紹了SpringBoot ResponseEntity標識Http響應方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07

