HttpSessionListener與Filter如何在Jetty9中使用

HttpSessionListener與Filter如何在Jetty9中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)主營沂源網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件定制開發(fā),沂源h5微信平臺小程序開發(fā)搭建,沂源網(wǎng)站營銷推廣歡迎沂源等地區(qū)企業(yè)咨詢

HttpSessionListener

當(dāng)Session創(chuàng)建或銷毀的時候被調(diào)用

示例代碼:

class MyHttpSessionListener implements HttpSessionListener { 
  @Override 
  public void sessionCreated(HttpSessionEvent httpSessionEvent) { 
    System.out.println("sessionCreated"); 
  } 
 
  @Override 
  public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { 
    System.out.println("sessionDestroyed"); 
  } 
} 

注冊方法:

ServletContextHandler.getSessionHandler().addEventListener(new MyHttpSessionListener()); 

注意:  若整個請求中都沒有用到Session, 則不會生成它, 也不會調(diào)用Listener

Filter

當(dāng)客戶端請求數(shù)據(jù)時被調(diào)用

示例代碼:

class MyFilter implements Filter { 
 
  public MyFilter() { 
 
  } 
 
  @Override 
  public void init(FilterConfig filterConfig) throws ServletException { 
 
  } 
 
  @Override 
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
    if (servletRequest instanceof HttpServletRequest) { 
      HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; 
 
      System.out.println(httpRequest.getServletPath()); 
    } 
    filterChain.doFilter(servletRequest, servletResponse); 
  } 
 
  @Override 
  public void destroy() { 
 
  } 
} 

注冊方法:

ServletContextHandler.addFilter(new FilterHolder(new MyFilter()), "/*", EnumSet.allOf(DispatcherType.class)); 

注意: 若請求的路徑錯誤, 則不會觸發(fā)Filter

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

文章名稱:HttpSessionListener與Filter如何在Jetty9中使用
網(wǎng)址分享:http://muchs.cn/article20/jioojo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站內(nèi)鏈標(biāo)簽優(yōu)化、小程序開發(fā)、微信小程序網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)