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 >
目录
相关文章
|
7月前
|
SQL 前端开发 Java
JSP软件产品管理系统myeclipse开发sql计算机程序web结构java编程网页源码
JSP软件产品管理系统是一套完善的毕业设计系统(servlet+dao+bean模式开发)MVC结构,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发
33 0
|
Java 应用服务中间件 程序员
无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]
无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]
1156 0
无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]
|
Java 应用服务中间件 数据格式
JSP脚本语法,第一个jsp程序案例,jsp页面脚本、指令、动作
JSP脚本语法,第一个jsp程序案例,jsp页面脚本、指令、动作
166 0
JSP脚本语法,第一个jsp程序案例,jsp页面脚本、指令、动作
|
关系型数据库 Linux 应用服务中间件
|
Web App开发 Java 应用服务中间件
《Servlet和JSP学习指南》一1.3 编写基础的Servlet应用程序
本节书摘来自华章出版社《Servlet和JSP学习指南》一书中的第1章,第1.3节,作者(加)Budi Kurniawan,更多章节内容可以访问云栖社区“华章计算机”公众号查看
1371 0
|
Java 数据安全/隐私保护 容器
|
Java 数据安全/隐私保护 容器
[Servlet&amp;JSP] 监听器的使用
在Servlet/JSP中除了ServletContextListener外,还有ServletRequestListener、HttpSessionListener等监听器,可以监听请求、会话对象生命周期;ServletRequestAttributeListener、HttpSessionAttributeListener、ServletContextAttributeL
1400 0
|
Web App开发 Java 应用服务中间件
MyEclipse2014配置Tomcat开发JavaWeb程序JSP以及Servlet
http://blog.csdn.net/21aspnet/article/details/21867241   1.安装准备 1).下载安装MyEclipse2014,这已经是最新版本。   2).
1305 0