牵着老婆满街逛

严以律己,宽以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

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

转载自:http://chengyue2007.iteye.com/blog/472431

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,
        ServletContextAttributeListener 
{

    
private ServletContext servletContext;

    
private String AttributeName;

    
private Object AttributeValue;

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

    }


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


    
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);

    }


    
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);
    }


    
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;

public class TimerTest {
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秒
}


static class MyTask extends java.util.TimerTask{
@Override
public void run() {

System.out.println(
"我是来测试的。。我两秒出来一次");
}

}

}



下面是xml的配置方法
<listener>
<listener-class>Unit.Test.ServletConextListener</listener-class>
</listener>

posted on 2012-05-04 12:56 杨粼波 阅读(774) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理