用TimerTask,下面的代码只是参考一下public class TouchWebXMLServlet
extends HttpServlet { private class TouchTask
extends TimerTask {
private String path;
private long begin; public TouchTask(String path, long begin) {
this.path = path;
this.begin = begin;
} public void run() {
if (isUpdate(new File(path))) {
File file = new File(path + File.separator + "web.xml");
file.setLastModified(System.currentTimeMillis());
}
} private boolean isUpdate(File path) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].lastModified() > begin) {
return true;
}
if (files[i].isDirectory() && isUpdate(files[i])) {
return true;
}
}
return false;
} } private TouchTask task; public void init() throws ServletException {
long begin = System.currentTimeMillis();
String path = this.getServletContext().getRealPath("WEB-INF");
task = new TouchTask(path, begin);
long interval = 10000;
try {
interval = Long.parseLong(this.getInitParameter("interval"));
}
finally {}
new Timer().schedule(task, 0, interval);
System.out.println("自动更新 WEB-INF 功能已加载");
} public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
} public void destroy() {
task.cancel();
System.out.println("自动更新 WEB-INF 功能已卸载");
}}
extends HttpServlet { private class TouchTask
extends TimerTask {
private String path;
private long begin; public TouchTask(String path, long begin) {
this.path = path;
this.begin = begin;
} public void run() {
if (isUpdate(new File(path))) {
File file = new File(path + File.separator + "web.xml");
file.setLastModified(System.currentTimeMillis());
}
} private boolean isUpdate(File path) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].lastModified() > begin) {
return true;
}
if (files[i].isDirectory() && isUpdate(files[i])) {
return true;
}
}
return false;
} } private TouchTask task; public void init() throws ServletException {
long begin = System.currentTimeMillis();
String path = this.getServletContext().getRealPath("WEB-INF");
task = new TouchTask(path, begin);
long interval = 10000;
try {
interval = Long.parseLong(this.getInitParameter("interval"));
}
finally {}
new Timer().schedule(task, 0, interval);
System.out.println("自动更新 WEB-INF 功能已加载");
} public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
} public void destroy() {
task.cancel();
System.out.println("自动更新 WEB-INF 功能已卸载");
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货