HttpSessionListener java java 在Jetty9中使用HttpSessionListener和Filter
人气:1想了解java 在Jetty9中使用HttpSessionListener和Filter的相关内容吗,在本文为您仔细讲解HttpSessionListener java 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,在Jetty9中使用HttpSessionListener和Filter,java,HttpSessionListener和Filter,下面大家一起来学习吧。
java 在Jetty9中使用HttpSessionListener和Filter
HttpSessionListener
当Session创建或销毁的时候被调用
示例代码:
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, 则不会生成它, 也不会调用Listener
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() { } }
注册方法:
ServletContextHandler.addFilter(new FilterHolder(new MyFilter()), "/*", EnumSet.allOf(DispatcherType.class));
注意: 若请求的路径错误, 则不会触发Filter
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
加载全部内容