這篇文章主要講解了“怎么應(yīng)用JavaEE的過濾器、監(jiān)聽、攔截技術(shù)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么應(yīng)用JavaEE的過濾器、監(jiān)聽、攔截技術(shù)”吧!
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供綏德網(wǎng)站建設(shè)、綏德做網(wǎng)站、綏德網(wǎng)站設(shè)計(jì)、綏德網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、綏德企業(yè)網(wǎng)站模板建站服務(wù),10余年綏德做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。JavaWeb三大組件:Servlet,Listener,F(xiàn)ilter。監(jiān)聽器就是指在應(yīng)用程序中監(jiān)聽相關(guān)對象狀態(tài)變化的組件。
指被監(jiān)聽對象。
ServletContext
ServletContextListener
生命周期監(jiān)聽,它有兩個(gè)方法,出生時(shí)調(diào)用contextInitialized()
,銷毀時(shí)調(diào)用contextDestroyed()
;
ServletContextAttributeListener
屬性監(jiān)聽,它有三個(gè)方法,添加屬性attributeAdded()
,替換屬性attributeReplaced()
,移除屬性時(shí)attributeRemoved()
。
HttpSession
HttpSessionListener
生命周期監(jiān)聽:它有兩個(gè)方法,出生時(shí)調(diào)用sessionCreated()
,銷毀時(shí)調(diào)用sessionDestroyed()
;
HttpSessioniAttributeListener
屬性監(jiān)聽:它有三個(gè)方法,添加屬性attributeAdded()
,替換屬性attributeReplaced()
,移除屬性attributeRemoved()
。
ServletRequest
ServletRequestListener
生命周期監(jiān)聽:它有兩個(gè)方法,出生時(shí)調(diào)用requestInitialized()
,銷毀時(shí)調(diào)用requestDestroyed()
;
ServletRequestAttributeListener
屬性監(jiān)聽:它有三個(gè)方法,添加屬性attributeAdded()
,替換屬性attributeReplaced()
,移除屬性attributeRemoved()
。
相關(guān)監(jiān)聽器
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()); } }
web.xml配置文件
<!-- 監(jiān)聽器相關(guān)配置 --> <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> <!-- 設(shè)置session失效時(shí)間為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初始化數(shù)據(jù) ServletContext servletContext = this.getServletContext() ; String author = String.valueOf(servletContext.getAttribute("author")) ; System.out.println("TheContextListener Author:"+author); // 2、Request屬性設(shè)置 request.setAttribute("mood","smile"); request.setAttribute("mood","agitated"); // 3、Session創(chuàng)建,1分鐘失效,調(diào)用銷毀 HttpSession session = request.getSession(true) ; session.setAttribute("casually","casually"); response.getWriter().print("Hello:Listener"); } }
客戶端請求Servlet時(shí),先執(zhí)行相關(guān)Filter,如果Filter通過,則繼承執(zhí)行請求的Servlet;如果Filter不通過,則不會(huì)執(zhí)行用戶請求的Servlet。過濾器可以動(dòng)態(tài)地?cái)r截請求和響應(yīng)。
Filter接口定義了三個(gè)核心方法。
init()
應(yīng)用程序啟動(dòng)時(shí),服務(wù)器實(shí)例化Filter對象,并調(diào)用其init方法,讀取web.xml配置,完成對象的初始化加載。
doFilter()
實(shí)際的過濾操作,請求達(dá)到服務(wù)器時(shí),Servlet容器將先調(diào)用過濾器的doFilter方法。
destroy()
容器在銷毀過濾器前調(diào)用該方法,釋放過濾器占用的資源。
編寫過濾器
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()"); } }
web.xml配置文件
<!-- 過濾器相關(guān)配置 --> <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,主要用于攔截用戶請求并作相應(yīng)的處理。例如通過攔截器可以進(jìn)行權(quán)限驗(yàn)證、記錄請求信息的日志、判斷用戶是否登錄等。請求轉(zhuǎn)發(fā)不執(zhí)行攔截、過濾;重定向執(zhí)行攔截和過濾。
感謝各位的閱讀,以上就是“怎么應(yīng)用JavaEE的過濾器、監(jiān)聽、攔截技術(shù)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么應(yīng)用JavaEE的過濾器、監(jiān)聽、攔截技術(shù)這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
當(dāng)前標(biāo)題:怎么應(yīng)用JavaEE的過濾器、監(jiān)聽、攔截技術(shù)-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://vcdvsql.cn/article24/dshoje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、定制開發(fā)、App開發(fā)、關(guān)鍵詞優(yōu)化、用戶體驗(yàn)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容