Java * 的作用及用法代码示例
作者:Chimomo 时间:2023-06-24 06:59:07
* 在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
统计网站访问量
实现访问监控
有时候后台需要统计最多在线人数以及当前在线人数,这里通过 * 实现这一功能。
图片仅供参考。。。
实例一
下面是一个统计网站最多在线人数 * 的例子
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);
}
}
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应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用 * 技术来实现这个功能了。
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);
}
}
}
来源:http://blog.csdn.net/troubleshooter/article/details/78416857
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android App应用启动分析与优化
![](https://img.aspxhome.com/file/2023/9/137419_0s.gif)
Java反转数组输出实例代码
Java ArrayList.toArray(T[]) 方法的参数类型是 T 而不是 E的原因分析
Android仿微信之界面导航篇(1)
![](https://img.aspxhome.com/file/2023/8/138398_0s.jpg)
Android使用多线程进行网络聊天室通信
![](https://img.aspxhome.com/file/2023/2/123262_0s.png)
JAVA中常见异常类
理解Android中Activity的方法回调
Android通过自定义ImageView控件实现图片的缩放和拖动的实现代码
Spring gateway + Oauth2实现单点登录及详细配置
![](https://img.aspxhome.com/file/2023/2/70942_0s.png)
java文件操作输入输出结构详解
springboot整合企微webhook机器人发送消息提醒
![](https://img.aspxhome.com/file/2023/2/64222_0s.jpg)
springboot依赖冲突问题及解决过程
![](https://img.aspxhome.com/file/2023/2/129252_0s.jpg)
java 获取日期的几天前,几个月前和几年前的实例
MybatisPlus,无XML分分钟实现CRUD操作
![](https://img.aspxhome.com/file/2023/1/108221_0s.jpg)
C#后台创建控件并获取值的方法
解读JDK8踩坑JCE加密限制版本问题
![](https://img.aspxhome.com/file/2023/2/96042_0s.png)
浅谈spring方法级参数校验(@Validated)
![](https://img.aspxhome.com/file/2023/7/90037_0s.png)
详解Android应用沙盒机制
SpringMVC请求数据详解讲解
Java中Set集合的使用和底层原理解析
![](https://img.aspxhome.com/file/2023/5/83105_0s.png)