jsp中监听器和定时器的结合的程序

package Unit.Test;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import test.TimerTest;

public  class ServletConextListener  implements ServletContextListener,
ExpandedBlockStart.gif        ServletContextAttributeListener  {

    private ServletContext servletContext;

    private String AttributeName;

    private Object AttributeValue;

ExpandedSubBlockStart.gif    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("####ServletContext上下文销毁");

    }

ExpandedSubBlockStart.gif    public void contextInitialized(ServletContextEvent sce) {
        servletContext = sce.getServletContext();
        
        TimerTest test=new TimerTest();
        //这个是定时器的内容
        
        System.out.println("####上下文初始化");
        
        System.out.println("####ServletContext当前初始目录为:"
                + this.servletContext.getRealPath("/"));
        
    }

ExpandedSubBlockStart.gif    public void attributeAdded(ServletContextAttributeEvent sce) {
        AttributeName = sce.getName();
        AttributeValue = sce.getValue();
        System.out.println("####ServletContext增加一个属性:");
        System.out.println("AttributeName:" + AttributeName);
        System.out.println("AttributeValue:" + AttributeValue);

    }

ExpandedSubBlockStart.gif    public void attributeRemoved(ServletContextAttributeEvent sce) {
        AttributeName = sce.getName();
        AttributeValue = sce.getValue();
        System.out.println("####ServletContext删除一个属性:");
        System.out.println("AttributeName:" + AttributeName);
        System.out.println("AttributeValue:" + AttributeValue);
    }

ExpandedSubBlockStart.gif    public void attributeReplaced(ServletContextAttributeEvent sce) {
        AttributeName = sce.getName();
        AttributeValue = sce.getValue();
        System.out.println("####ServletContext替换一个属性:");
        System.out.println("AttributeName:" + AttributeName);
        System.out.println("AttributeValue:" + AttributeValue);
    }

}
上面是监听器的内容,下面是定时器的内容

package test;

import java.io.IOException;
import java.util.Date;
import java.util.Timer;

ExpandedBlockStart.gif public  class TimerTest  {
ExpandedSubBlockStart.gif public TimerTest(){
 Date time=new Date();
 time.setHours(17);
 time.setMinutes(48);
 time.setSeconds(20);
 Timer timer = new Timer();
 timer.schedule(new MyTask(),time);//在1秒后执行此任务,每次间隔2秒
}

ExpandedSubBlockStart.gifstatic class MyTask extends java.util.TimerTask{
 @Override
ExpandedSubBlockStart.gif public void run() {
 
 System.out.println("我是来测试的。。我两秒出来一次");
 }
}
}


下面是xml的配置方法
< listener >
  < listener-class >Unit.Test.ServletConextListener </ listener-class >
</ listener >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值