Home >Java >javaTutorial >java Detailed explanation of using HttpSessionListener and Filter instances in Jetty9
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
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
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!