| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2365 人关注过本帖
标题:[分享]学习笔记之tomcat启动自动运行类
只看楼主 加入收藏
gameovercn
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2005-10-25
收藏
 问题点数:0 回复次数:1 
[分享]学习笔记之tomcat启动自动运行类
首先编写一个servlet类--Startup.java
将需要自动运行的操作代码写在init方法中。
如下配置web.xml

<servlet>
<servlet-name>startup</servlet-name>
<servlet-class>com.usease.Startup</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

附servlet全代码:
package com.usease;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Startup extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables
public void init() throws ServletException {
try {
File file = new File("d:/test.txt");
int count = 0;
if (file.exists()) {
BufferedReader in = new BufferedReader(new FileReader(file));
String inString = in.readLine();
count = Integer.parseInt(inString);
in.close();
}
count++;
PrintWriter out = new PrintWriter(new FileOutputStream("d:/test.txt"));
out.print(count);
out.close();
}
catch (IOException e) {}
System.out.println("Count completed!");
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}

//Clean up resources
public void destroy() {
}
}

这样,当Tomcat加载这个Context路径的时候,就会自动相应的操作了。
搜索更多相关主题的帖子: tomcat 笔记 自动 运行 分享 
2006-06-07 09:10
RealHector
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-17
收藏
得分:0 
这个是每启动一次就运行一次代码么?
能不能设置为每天运行一次
2011-05-03 16:14
快速回复:[分享]学习笔记之tomcat启动自动运行类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025453 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved