手机监听器好用吗:用servlet 实现监听器

用 servlet 实现监听器工作需要,对监听器的实现做了研究学习,记录如下

监听器的种类很多,基本分类如下图的JB2006的图式就可以看出来.......晕 无法添加图片 ???


基本分为 对 context session request 。


基本事件做下面的关系图说明

context —— ServletContextListener

—— ServletAttributeContextListener

session —— SessionContextListener
—— SessionAttributeContextListener

request —— RequestContextListener
—— RequestAttributeContextListener

对于每个监听器,分别继承了不同的方法,如下

contextInitialized // servlet 容器被加载的时候
contextDestroyed // servlet 容器被撤销的时候
attributeAdded // 添加 servlet 属性时
attributeRemoved // 移除 servlet 属性时
attributeReplaced // 更新 servlet 属性时

sessionCreated //略
sessionDestroyed
attributeAdded
attributeRemoved
attributeReplaced

requestInitialized
requestDestroyed
attributeAdded
attributeRemoved
attributeReplaced

最后,将实现的类添加到应用服务的 web.xml中:
  
  <listener>
  <listener-class>类路径</listener-class>
  </listener>

通过以上的方法实现,我们实现了对服务器事件内容的响应,但这还不是我们预期的结果。

我们的原始需求,要做一个定时监听器,在服务启动的时候,可以同时运行起来,并且定

时监听其他服务器内容的更改,在服务关掉的时候,停止监听 。以上只是实现了监听事件

与服务事件的响应,接下来,实现事件与时间的相互响应。

首先建立一个服务与事件的相应的应用实现

import java.util.*;
public class appServerEven {
private final Timer timer = new Timer(); // 建立时间监听对象
public appServerEven() { }
public void start() {
timer.schedule(事件处理类, 开始时间, 时间间隔);
/**
Date date = new Date(); // 系统当前时间
timer.schedule(new appEven() , date, 分钟 * 60 * 1000);
*/
}
public void stop() {
timer.cancel();
}
}

其次,建立事件与时间的响应事件

import java.util.*;
import java.text.SimpleDateFormat;

public class ReplyTask extends TimerTask // 扩展实现TimeTask 时间任务
{
public void run() {
// 根据时间,进行的事物处理的
/**
// 输出当前的系统时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String starttime = sdf.format(new Date());
System.out.println(starttime);


*/
}
}

通过将上面的"时间",修改为具体数据,我们就实现了一个在WEB应用服务启动的时候自动启动,

并且,每隔几分钟自动输出系统当前时间到界面的监听器.
Tags:  servlet

延伸阅读

最新评论

发表评论