java web(五):java web三大組件之另外兩個和八大監聽器
java的三大組件指Servlet、Filter、Listener。八大監聽器指八個接口。前面介紹了Servlet,現在介紹一下Filter攔截器以及攔截地址的設置,
Listener監聽那些事件。
java web的cookie和session機制有篇博客講的很好,有興趣的博友可以去看看。地址:https://www.cnblogs.com/whgk/p/6422391.html
一:Filter
1.基本概念
Filter稱之為過濾器,是用來做一些攔截的任務, 在Servlet接受請求之前,做一些事情,如果不滿足限定,可以拒絕進入Servlet。
一個web項目中可以配置多個filter過濾器,瀏覽器訪問靜態資源如html、jsp、css或者訪問動態資源servlet都會經過filter過濾器,滿足條件,過濾器放行,否則直接返回。
2.使用
filter有很多用處,網上一搜,在filter層,來獲取用戶的身份,可以考慮在filter層做一些常規的校驗(如參數校驗,referer校驗等),可以在filter層做
穩定性相關的工作(如全鏈路打點,可以在filter層分配一個traceId;也可以在這一層做限流等)。我最常見的是spring中的編碼過濾器。我們通過編碼過濾器認識filter。
代碼:
package com.briup.servlet.filter;
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;
/**
* Servlet Filter implementation class EncodingFilter
* 編碼過濾器
*/
public class EncodingFilter implements Filter {
private String encoding;
public EncodingFilter() {}
public void destroy() {}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
//設置編碼格式【只對post方式有效】;
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
//放行;
chain.doFilter(request, response);
System.out.println("servlet執行完畢,返回到filter");
}
public void init(FilterConfig fConfig) throws ServletException {
//我們把編碼設置在web.xml中,如果需要改編碼在配置文件中更改而不需要更改代碼
encoding = fConfig.getInitParameter("encoding");
}
}
web.xml:
<filter>
<display-name>EncodingFilter</display-name>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.briup.servlet.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<!-- /* 代表攔截所有請求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
訪問:http://127.0.0.1:7778/StudyServlet/HelloWorld或者http://127.0.0.1:7778/StudyServlet/success.html,控制臺都會輸出一句:"servlet執行完畢,返回到filter"。
3.攔截地址的配置
filter我主要想說的就是攔截地址如何配置了。
第一種 【匹配任意】
<url-pattern>/*</url-pattern>
第二種 【精確匹配】
<url-pattern>/test_servlet</url-pattern>
表示此攔截器只會攔截/test_servlet這一個路徑
第三種 【擴展名匹配】
<url-pattern>*.html</url-pattern>
表示此攔截器只會攔截後綴名是.html的路徑
第四種 【路徑匹配】
<url-pattern>/test/*</url-pattern>
表示此攔截器攔截/test路徑下的所有資源
註意:服務器內部跳轉不會攔截,只會攔截瀏覽器發送的地址。
二:監聽器
Servlet監聽器的作用是監聽Web容器的有效期事件,可以監聽由於Web應用中狀態改變而引起的Servlet容器產生的相應事件,然後接受並處理這些事件。
下面簡單介紹這8個接口和其中的方法。
監聽ServletContent(應用上下文)
1.ServletContentListener接口
Servlet的上下文監聽,它主要實現監聽ServletContext的創建和刪除
(1)contextInitialized(ServletContextEvent event); //通知正在收聽的對象,應用程序已經被加載和初始化。
(2)contextDestroyed(ServletCotextEvent event); // 通知正在收聽的對象,應用程序已經被載出,即關閉。
2.ServletContextAttributeListener接口
應用上下文存、移除、更改數據進行監聽。
(1)attributeAdded(ServletContextAttributeEvent event); //應用上下文存數據的時候觸發【調用setAttribute方法】
(2)attributeRemoved(ServletContextAttributeEvent event); //ServletContent對象調用removeAttribute方法觸發。
(3)attributeReplaced(ServletContextAttributeEvent event); //當存數據的時候,key值已經存在,value值被替換的時候觸發。
監聽session接口
3.HttpSessionListener接口
(1)sessionCreated(HttpSessionEvent even); //session被創建的時候觸發。
(2)sessionDestroyed(HttpSessionEvent event); //session過期失效觸發
4.HttpSessionAttributeListener
用法和ServletContextAttributeListener接口類似。
監聽request接口
5.ServletRequestListener
6.ServletRequestAttributeListener
用法和上面類似。
7.HttpSessionActivationListener;
該接口實現監聽HTTP會話active和passivate。
(1)attributeAdded(HttpSessionBindingEvent event); // 當有對象加入session的範圍時,通知正在收聽的對象
(2)attributeReplaced(HttpSessionBindingEvent event); //當在session的範圍有對象取代另一個對象時,通知正在收聽的對象。
(3)attributeRemoved(HttpSessionBindingEvent event); //當有對象從session的範圍有對象取代另一個對象時,通知正在收聽的對象 。
其中HttpSessionBindingEvent類主要有三個方法:getName()、getSession()和getValue()
8.HttpBindingListener;
接口實現監聽HTTP會話中對象的綁定信息。
(1)alueBound(HttpSessionBindingEvent event); //當有對象加入session的範圍時會被自動調用
(2)valueUnBound(HttpSessionBindingEvent event); //當有對象從session的範圍內移除時會被自動調用
註:本文一部分是參考網上資料的。
java web(五):java web三大組件之另外兩個和八大監聽器