Home  >  Article  >  Java  >  java Detailed explanation of using HttpSessionListener and Filter instances in Jetty9

java Detailed explanation of using HttpSessionListener and Filter instances in Jetty9

怪我咯
怪我咯Original
2017-06-30 10:33:311489browse

This article mainly introduces the related information of Java using HttpSessionListener and Filter in Jetty9. Friends who need it can refer to

java Using HttpSessionListener in Jetty9 And Filter

##HttpSessionListener

is called when the Session is created or destroyed

Sample code:

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

Registration method:

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

Note: If Session is not used in the entire request, it will not be generated and Listener will not be called

Filter

Called when the client requests data

Sample code:

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() { 
 
  } 
}

Registration method:

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

Note: If the requested path is wrong, the Filter will not be triggered

The above is the detailed content of java Detailed explanation of using HttpSessionListener and Filter instances in Jetty9. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn