软件编程
位置:首页>> 软件编程>> java编程>> Java * 的作用及用法代码示例

Java * 的作用及用法代码示例

作者:Chimomo  发布时间:2023-06-24 06:59:07 

标签:java, ,

* 在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 * 的作用及用法代码示例

图片仅供参考。。。

实例一

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


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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com