java 在Jetty9中使用HttpSessionListener和Filter

2025-05-29 0 56

java 在Jetty9中使用HttpSessionListenerFilter

HttpSessionListener

当Session创建或销毁的时候被调用

示例代码:

?

1

2

3

4

5

6

7

8

9

10

11
class MyHttpSessionListener implements HttpSessionListener {

@Override

public void sessionCreated(HttpSessionEvent httpSessionEvent) {

System.out.println("sessionCreated");

}

@Override

public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {

System.out.println("sessionDestroyed");

}

}

注册方法:

?

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

注意: 若整个请求中都没有用到Session, 则不会生成它, 也不会调用Listener

Filter

当客户端请求数据时被调用

示例代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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

}

}

注册方法:

?

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

注意: 若请求的路径错误, 则不会触发Filter

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/sidyhe/article/details/52084558

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java 在Jetty9中使用HttpSessionListener和Filter https://www.kuaiidc.com/116030.html

相关文章

发表评论
暂无评论