Home > Java > javaTutorial > body text

java Detailed explanation of using HttpSessionListener and Filter instances in Jetty9

怪我咯
Release: 2017-06-30 10:33:31
Original
1433 people have browsed it

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"); 
  } 
}
Copy after login

Registration method:

ServletContextHandler.getSessionHandler().addEventListener(new MyHttpSessionListener());
Copy after login

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

Registration method:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!