如题,自己搜了很多,讲的乱七八糟。实验没有出现每隔一段时间出现的效果,郁闷中
解决方案 »
- Spring shiro 配置问题!~~求助进来看看 谢谢
- JAVA的一个小问题.
- java web 怎么给自动给用户反馈。
- CS问题
- 如何实现JAVA窗口的右下角出现
- 用jsp做的大型的购物网站后台的mysql数据库怎么设计
- 农村人请教,为什么不能关闭pstm.close();呢,一关闭就出现了 java.lang.NullPointerException
- csdn高手来帮帮我Sessionbean引用CMP问题
- 急急急!!!!!!一个sessionListener的问题
- 怎么知道mysql使用什么中文编码方式(charsetEncoding)?或者说怎么设置中文编码方式?
- SSH ,org.objectweb.asm.ClassVisitor.visit异常
- 刚刚学习Jsp 谁能说说JavaBean连接数据库跟Servlet连接数据库有什么区别吗
<servlet>
<servlet-name>taskservlet</servlet-name>
<servlet-class>com.ApplicationCache</servlet-class>
<init-param>
<param-name>startTask</param-name>
<param-value>true</param-value>
</init-param >
<init-param >
<param-name>intervalTime</param-name>
<param-value>20</param-value>
</init-param >
<load-on-startup>300</load-on-startup>
</servlet>package com;
import java.io.IOException;
import java.util.Timer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
public class ApplicationCache extends HttpServlet {
private static final long serialVersionUID = 1L;
private Timer timer1 = null;
private Task task1;
/**
* Constructor of the object.
*/
public ApplicationCache() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy();
if(timer1!=null){
timer1.cancel();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
// init方法启动定时器
public void init() throws ServletException {
ServletContext context = getServletContext();
//HttpSession session = request.
// (true为用定时间刷新缓存)
String startTask = getInitParameter("startTask");
// 定时刷新时间(分钟)
Long delay = Long.parseLong(getInitParameter("intervalTime"));
// 启动定时器
if(startTask.equals("true")){
timer1 = new Timer(true);
task1 = new Task(context);
// timer1.schedule(task1, delay * 60 * 1000, delay * 60 * 1000);
timer1.schedule(task1, 10000, delay*60*1000);
}
}
}
public class Task extends TimerTask { private ServletContext context; private static boolean isRunning = true; public Task(ServletContext context) {
this.context = context;
} @Override
public void run() {
if (isRunning) { saveCache();
}
} public void saveCache() {
//这里写操作 }
}