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);
}
}
上面是监听器的内容,下面是定时器的内容
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("我是来测试的。。我两秒出来一次");
}
}
}
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 >
< listener-class >Unit.Test.ServletConextListener </ listener-class >
</ listener >