Java监听器的作用及用法代码示例

2025-05-27 0 60

监听器JavaWeb开发中用得比较多

Java Web开发中的监听器(listener)就是application、session、request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件,如下所示:

①ServletContextListener:对Servlet上下文的创建和销毁进行监听。
②ServletContextAttributeListener:监听Servlet上下文属性的添加、删除和替换。
③HttpSessionListener:对Session的创建和销毁进行监听。

补充:session的销毁有两种情况:1). session超时(可以在web.xml中通过<session-config>/<session-timeout>标签配置超时时间);2). 通过调用session对象的invalidate()方法使session失效。

④HttpSessionAttributeListener:对Session对象中属性的添加、删除和替换进行监听。
⑤ServletRequestListener:对请求对象的初始化和销毁进行监听。
⑥ServletRequestAttributeListener:对请求对象属性的添加、删除和替换进行监听。

监听器常用的用途

它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量

通常使用Web监听器做以下的内容:

  统计在线人数,利用HttpSessionLisener

  加载初始化信息:利用ServletContextListener

  统计网站访问量

  实现访问监控

有时候后台需要统计最多在线人数以及当前在线人数,这里通过监听器实现这一功能。

Java监听器的作用及用法代码示例

图片仅供参考。。。

实例一

下面是一个统计网站最多在线人数监听器的例子

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

import javax.servlet.annotation.WebListener;

/**

上下文监听器,在服务器启动时初始化onLineCount和maxOnLineCount两个变量

并将其置于服务器上下文(ServletContext)中,其初始值都是0

*/

@WebListener

public class InitListener implements ServletContextListener {

@Override

public void contextDestroyed(ServletContextEvent evt) {

}

@Override

public void contextInitialized(ServletContextEvent evt) {

evt.getServletContext().setAttribute("onLineCount", 0);

evt.getServletContext().setAttribute("maxOnLineCount", 0);

}

}

?

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

27

28

29

30

31

32

33

34
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.ServletContext;

import javax.servlet.annotation.WebListener;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

/**

会话监听器,在用户会话创建和销毁的时候根据情况

修改onLineCount和maxOnLineCount的值

*/

@WebListener

public class MaxCountListener implements HttpSessionListener {

@Override

public void sessionCreated(HttpSessionEvent event) {

ServletContext ctx = event.getSession().getServletContext();

int count = Integer.parseInt(ctx.getAttribute("onLineCount").toString());

count++;

ctx.setAttribute("onLineCount", count);

int maxOnLineCount = Integer.parseInt(ctx.getAttribute("maxOnLineCount").toString());

if (count > maxOnLineCount) {

ctx.setAttribute("maxOnLineCount", count);

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

ctx.setAttribute("date", df.format(new Date()));

}

}

@Override

public void sessionDestroyed(HttpSessionEvent event) {

ServletContext app = event.getSession().getServletContext();

int count = Integer.parseInt(app.getAttribute("onLineCount").toString());

count--;

app.setAttribute("onLineCount", count);

}

}

说明:这里使用了Servlet 3规范中的@WebListener注解配置监听器,当然你可以在web.xml文件中用<listener>标签配置监听器

实例二

JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听器技术来实现这个功能了。

?

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

27

28

29

30

31

32

33

34

35
package me.gacl.web.listener;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

/**

* @ClassName: OnLineCountListener

* @Description: 统计当前在线用户个数

* @author: 孤傲苍狼

* @date: 2014-9-10 下午10:01:26

*

*/

public class OnLineCountListener implements HttpSessionListener {

@Override

public void sessionCreated(HttpSessionEvent se) {

ServletContext context = se.getSession().getServletContext();

Integer onLineCount = (Integer) context.getAttribute("onLineCount");

if(onLineCount==null){

context.setAttribute("onLineCount", 1);

}else{

onLineCount++;

context.setAttribute("onLineCount", onLineCount);

}

}

@Override

public void sessionDestroyed(HttpSessionEvent se) {

ServletContext context = se.getSession().getServletContext();

Integer onLineCount = (Integer) context.getAttribute("onLineCount");

if(onLineCount==null){

context.setAttribute("onLineCount", 1);

}else{

onLineCount--;

context.setAttribute("onLineCount", onLineCount);

}

}

}

总结

以上就是本文关于Java监听器的作用及用法代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们长久以来对本站的支持!

原文链接:http://blog.csdn.net/troubleshooter/article/details/78416857

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java监听器的作用及用法代码示例 https://www.kuaiidc.com/77580.html

相关文章

发表评论
暂无评论