jsp+JS的问题

来源:百度知道 编辑:UC知道 时间:2024/06/01 12:03:53
我的意思是:
在服务器端需要设计一个定时执行的程序,功能是对JSP里的一个application.setAttribute("number",0);
字段进行改变.能使用web.xml里加入线程类,但是我不会,请求帮助!

可以添加一个filter,在filter的init()方法中创建一个定时器(java的Timer类:java.util.Timer),然后在web.xml中加上这个filter。
web application 启动的时候,会执行一次filter的init()方法(在整个web app生命周期只初始化一次)。
下面是个例子:
private java.util.Timer timer;
timer = new Timer(true);
timer.schedule(new java.util.TimerTask() {
public void run() {
//do something here }
}, 0, 5*60*1000);
应该可以达到要求。
注:应该还有其他解决方法,期待高明的方法!

private java.util.Timer timer;
timer = new Timer(true);
timer.schedule(new java.util.TimerTask() {
public void run() {
//do something here }
}, 0, 5*60*1000);