您好,登錄后才能下訂單哦!
本文源碼:GitHub·點這里 || GitEE·點這里
JavaWeb三大組件:Servlet,Listener,Filter。監聽器就是指在應用程序中監聽相關對象狀態變化的組件。
指被監聽對象。
ServletContextListener
生命周期監聽,它有兩個方法,出生時調用contextInitialized()
,銷毀時調用contextDestroyed()
;
ServletContextAttributeListener
屬性監聽,它有三個方法,添加屬性attributeAdded()
,替換屬性attributeReplaced()
,移除屬性時attributeRemoved()
。
HttpSessionListener
生命周期監聽:它有兩個方法,出生時調用sessionCreated()
,銷毀時調用sessionDestroyed()
;
HttpSessioniAttributeListener
屬性監聽:它有三個方法,添加屬性attributeAdded()
,替換屬性attributeReplaced()
,移除屬性attributeRemoved()
。
ServletRequestListener
生命周期監聽:它有兩個方法,出生時調用requestInitialized()
,銷毀時調用requestDestroyed()
;
ServletRequestAttributeListener
屬性監聽:它有三個方法,添加屬性attributeAdded()
,替換屬性attributeReplaced()
,移除屬性attributeRemoved()
。
TheContextListener
public class TheContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("初始化:TheContextListener");
ServletContext servletContext = servletContextEvent.getServletContext() ;
servletContext.setAttribute("author","cicada");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("銷毀:TheContextListener");
}
}
TheRequestListener
public class TheRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
System.out.println("初始化:TheRequestListener");
}
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
System.out.println("銷毀:TheRequestListener");
}
}
TheSessionListener
public class TheSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("初始化:TheSessionListener");
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("銷毀:TheSessionListener");
}
}
RequestAttributeListener
public class RequestAttributeListener implements ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent evt) {
System.out.println("Request添加屬性:"+evt.getName()+";"+evt.getValue());
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent evt) {
System.out.println("Request移除屬性:"+evt.getName()+";"+evt.getValue());
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent evt) {
System.out.println("Request替換屬性:"+evt.getName()+";"+evt.getValue());
}
}
<!-- 監聽器相關配置 -->
<listener>
<listener-class>com.node05.servlet.listener.TheContextListener</listener-class>
</listener>
<listener>
<listener-class>com.node05.servlet.listener.TheSessionListener</listener-class>
</listener>
<listener>
<listener-class>com.node05.servlet.listener.TheRequestListener</listener-class>
</listener>
<listener>
<listener-class>com.node05.servlet.listener.RequestAttributeListener</listener-class>
</listener>
<session-config>
<!-- 設置session失效時間為1分鐘 -->
<session-timeout>1</session-timeout>
</session-config>
public class ListenerServletImpl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
// 1、獲取TheContextListener初始化數據
ServletContext servletContext = this.getServletContext() ;
String author = String.valueOf(servletContext.getAttribute("author")) ;
System.out.println("TheContextListener Author:"+author);
// 2、Request屬性設置
request.setAttribute("mood","smile");
request.setAttribute("mood","agitated");
// 3、Session創建,1分鐘失效,調用銷毀
HttpSession session = request.getSession(true) ;
session.setAttribute("casually","casually");
response.getWriter().print("Hello:Listener");
}
}
客戶端請求Servlet時,先執行相關Filter,如果Filter通過,則繼承執行請求的Servlet;如果Filter不通過,則不會執行用戶請求的Servlet。過濾器可以動態地攔截請求和響應。
Filter接口定義了三個核心方法。
應用程序啟動時,服務器實例化Filter對象,并調用其init方法,讀取web.xml配置,完成對象的初始化加載。
實際的過濾操作,請求達到服務器時,Servlet容器將先調用過濾器的doFilter方法。
容器在銷毀過濾器前調用該方法,釋放過濾器占用的資源。
public class ThePrintLogFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String myName = filterConfig.getInitParameter("myName") ;
System.out.println("myName:"+myName);
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest ;
HttpServletResponse response = (HttpServletResponse)servletResponse ;
String name = request.getParameter("name") ;
if (!name.equals("cicada")){
response.getWriter().print("User Error !");
return ;
}
chain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("ThePrintLogFilter destroy()");
}
}
<!-- 過濾器相關配置 -->
<filter>
<filter-name>thePrintLogFilter</filter-name>
<filter-class>com.node05.servlet.filter.ThePrintLogFilter</filter-class>
<init-param>
<param-name>myName</param-name>
<param-value>cicada</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>thePrintLogFilter</filter-name>
<url-pattern>/filterServletImpl</url-pattern>
</filter-mapping>
public class FilterServletImpl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("Hello:Filter");
}
}
Spring框架中的攔截器Interceptor類似于Servlet中的過濾器Filter,主要用于攔截用戶請求并作相應的處理。例如通過攔截器可以進行權限驗證、記錄請求信息的日志、判斷用戶是否登錄等。請求轉發不執行攔截、過濾;重定向執行攔截和過濾。
GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。