web.xml配置监听定时器代码======================================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Name</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring ApplicationContext配置文件的路径可使用*通配符,多个路径用*,号分隔此参数用于后面的Spring-Context loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
</context-param>
<!-- 全局变量-->
<context-param>
<param-name>appValue</param-name>
<param-value>false</param-value>
</context-param>
<!--配置过滤器-->
<filter>
<filter-name>ActionFilter</filter-name>
<!--过滤类文件路径-->
<filter-class>com.xx.ActionFilter</filter-class>
<init-param>
<param-name>xx</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ActionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--加载spring配置文件applicationContext.xml-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--项目启动 开启监听对应的类文件-->
<listener>
<listener-class>taskListener</listener-class>
</listener>
</web-app>
JSP文件代码====================================================================
//ajax操作
$(function() {
$("#toSynchronize").click(function(){
//var activity_no = $("#activity_no").val();
$.post("${path}/xx/xx_messageMethod.do",
//{ "activity_no":"activity_no"},
function(data){
if(data=="ex"){
alert("可以进行操作!");
}else{
alert(今天已经操作过一次,请明天再警醒操作!);
}
}
)
});
});
<input type="button" name="toSynchronize" id="toSynchronize" value="一天只可操作一次"/>
ACTION接收AJAX信息============================================================
//返回提示信息,是否可进行操作
public String messageMethod(){
request=ServletActionContext.getRequest();
//当天是否还可以进行操作状态false:可以操作true:不可以操作
//全局是否存在变量toSynchronizeType
String toSynchronizeType=(String) ServletActionContext.getServletContext().getAttribute("toSynchronizeType");
//从配置文件web.xml中取得初始全局变量toSynchronizeType
String initToSynchronizeType=ServletActionContext.getServletContext().getInitParameter("toSynchronizeType");
boolean blean=false;
try{
if(toSynchronizeType!=null && !"".equals(toSynchronizeType)){
//如果全局中存在toSynchronizeType全局变量,并且值为false是 返回可操作提示信息
//同时设定变量值为true
if(toSynchronizeType.equals("false")){
ServletActionContext.getServletContext().setAttribute("toSynchronizeType", "true");
//要进行的操作编码省略
ServletActionContext.getResponse().getWriter().print("ex");
//如果为true,返回不可操作信息
}else{
ServletActionContext.getResponse().getWriter().print("notEx");
}
//如果全局中不存在toSynchronizeType变量,则判断初始全局变量,如果为falae,则返回可操作提示信息
//同时给定全局变量为true
}else{
if(initToSynchronizeType.equals("false")){
ServletActionContext.getServletContext().setAttribute("toSynchronizeType", "true");
//要进行的操作编码省略
ServletActionContext.getResponse().getWriter().print("ex");
}
}
}catch(Exception e){
blean=false;
e.printStackTrace();
}
request.setAttribute("blean", Boolean.toString(blean));
return null;
}
监听定时器类=========================================================================
import java.util.Timer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 监听定时器
* 实现ServletContextListener
*
*/
public class taskListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent event) {
}
public void contextInitialized(ServletContextEvent contextEvent) {
//获得ServletContext
ServletContext servletContext = contextEvent.getServletContext();
//取得web.xml文件中的初始变量值
String toSynchronizeType=servletContext.getInitParameter("toSynchronizeType");
//声明定时器
Timer timer = new Timer();
//每小时执行一次任务
timer.schedule(new task(servletContext),0,3600*1000);
}
}
定时任务类===========================================================================
import java.util.Calendar;
import java.util.TimerTask;
import javax.servlet.ServletContext;
/**
* 定时器任务类
*
*
*/
public class task extends TimerTask {
ServletContext servletContext=null;
private static final int C_SCHEDULE_HOUR = 0;
//构造函数
public task(ServletContext servletContext){
this.servletContext=servletContext;
}
public void run() {
Calendar cal = Calendar.getInstance();
//每天零点执行更新
if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY))
{
try
{
System.err.println("定时任务开始==========================");
//取得全局变量
String toSynchronize=(String) servletContext.getAttribute("toSynchronizeType");
System.out.println("现是否已經作状态="+toSynchronize);
if(toSynchronize!=null && !toSynchronize.equals("")){
System.out.println("正在重置状态=====================");
if(toSynchronize.equals("true")){
servletContext.setAttribute("toSynchronizeType", "false");
System.err.println("状态已经重置,可以进行操作了.........");
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Name</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring ApplicationContext配置文件的路径可使用*通配符,多个路径用*,号分隔此参数用于后面的Spring-Context loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
</context-param>
<!-- 全局变量-->
<context-param>
<param-name>appValue</param-name>
<param-value>false</param-value>
</context-param>
<!--配置过滤器-->
<filter>
<filter-name>ActionFilter</filter-name>
<!--过滤类文件路径-->
<filter-class>com.xx.ActionFilter</filter-class>
<init-param>
<param-name>xx</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ActionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--加载spring配置文件applicationContext.xml-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--项目启动 开启监听对应的类文件-->
<listener>
<listener-class>taskListener</listener-class>
</listener>
</web-app>
JSP文件代码====================================================================
//ajax操作
$(function() {
$("#toSynchronize").click(function(){
//var activity_no = $("#activity_no").val();
$.post("${path}/xx/xx_messageMethod.do",
//{ "activity_no":"activity_no"},
function(data){
if(data=="ex"){
alert("可以进行操作!");
}else{
alert(今天已经操作过一次,请明天再警醒操作!);
}
}
)
});
});
<input type="button" name="toSynchronize" id="toSynchronize" value="一天只可操作一次"/>
ACTION接收AJAX信息============================================================
//返回提示信息,是否可进行操作
public String messageMethod(){
request=ServletActionContext.getRequest();
//当天是否还可以进行操作状态false:可以操作true:不可以操作
//全局是否存在变量toSynchronizeType
String toSynchronizeType=(String) ServletActionContext.getServletContext().getAttribute("toSynchronizeType");
//从配置文件web.xml中取得初始全局变量toSynchronizeType
String initToSynchronizeType=ServletActionContext.getServletContext().getInitParameter("toSynchronizeType");
boolean blean=false;
try{
if(toSynchronizeType!=null && !"".equals(toSynchronizeType)){
//如果全局中存在toSynchronizeType全局变量,并且值为false是 返回可操作提示信息
//同时设定变量值为true
if(toSynchronizeType.equals("false")){
ServletActionContext.getServletContext().setAttribute("toSynchronizeType", "true");
//要进行的操作编码省略
ServletActionContext.getResponse().getWriter().print("ex");
//如果为true,返回不可操作信息
}else{
ServletActionContext.getResponse().getWriter().print("notEx");
}
//如果全局中不存在toSynchronizeType变量,则判断初始全局变量,如果为falae,则返回可操作提示信息
//同时给定全局变量为true
}else{
if(initToSynchronizeType.equals("false")){
ServletActionContext.getServletContext().setAttribute("toSynchronizeType", "true");
//要进行的操作编码省略
ServletActionContext.getResponse().getWriter().print("ex");
}
}
}catch(Exception e){
blean=false;
e.printStackTrace();
}
request.setAttribute("blean", Boolean.toString(blean));
return null;
}
监听定时器类=========================================================================
import java.util.Timer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 监听定时器
* 实现ServletContextListener
*
*/
public class taskListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent event) {
}
public void contextInitialized(ServletContextEvent contextEvent) {
//获得ServletContext
ServletContext servletContext = contextEvent.getServletContext();
//取得web.xml文件中的初始变量值
String toSynchronizeType=servletContext.getInitParameter("toSynchronizeType");
//声明定时器
Timer timer = new Timer();
//每小时执行一次任务
timer.schedule(new task(servletContext),0,3600*1000);
}
}
定时任务类===========================================================================
import java.util.Calendar;
import java.util.TimerTask;
import javax.servlet.ServletContext;
/**
* 定时器任务类
*
*
*/
public class task extends TimerTask {
ServletContext servletContext=null;
private static final int C_SCHEDULE_HOUR = 0;
//构造函数
public task(ServletContext servletContext){
this.servletContext=servletContext;
}
public void run() {
Calendar cal = Calendar.getInstance();
//每天零点执行更新
if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY))
{
try
{
System.err.println("定时任务开始==========================");
//取得全局变量
String toSynchronize=(String) servletContext.getAttribute("toSynchronizeType");
System.out.println("现是否已經作状态="+toSynchronize);
if(toSynchronize!=null && !toSynchronize.equals("")){
System.out.println("正在重置状态=====================");
if(toSynchronize.equals("true")){
servletContext.setAttribute("toSynchronizeType", "false");
System.err.println("状态已经重置,可以进行操作了.........");
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}