tomcat启动后让servlet一直运行
2013-01-10 11:45
477 查看
.1.web.xml 中设置servlet为<load-on-startup>1</load-on-startup>
2.AutoRunService在init方法中执行启动业务线程
3.AutoRunThread 定时运行
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>AutoRunService</servlet-name> <servlet-class>AutoRunService</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AutoRunService</servlet-name> <url-pattern>/servlet/AutoRunService</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
2.AutoRunService在init方法中执行启动业务线程
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AutoRunService extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * Constructor of the object. */ public AutoRunService() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here AutoRunThread autoRunThread = new AutoRunThread(); autoRunThread.start(); } }
3.AutoRunThread 定时运行
/** * */ /** * @author baijd * */ public class AutoRunThread extends Thread { public AutoRunThread() { super(); // TODO Auto-generated constructor stub } public void run() { try { do { // do something System.out.println("定时执行...."); // sleep(60 * 1000);这儿写的是2小时 sleep(2 * 1000); } while (true); } catch (Exception e) { } } }
相关文章推荐
- tomcat启动后让servlet一直运行
- eclipse下运行servlet启动tomcat时提示端口被占用
- tomcat启动服务运行servlet
- Servlet+Tomcat制作出第一个运行在Tomcat上的Java应用程序
- Tomcat 7启动自身项目报错,部署项目可正常运行(未解决)
- ubuntu下启动、关闭tomcat,查看tomcat运行日志
- Servlet在启动时加载的tomcat源码(原创)
- eclipse中复制servlet导致tomcat启动不了的问题
- 部署项目到Tomcat上后,启动项目时,提示多个不存在的其他项目运行失败
- 启动Tomcat时显示以下错误,影响系统运行吗?
- eclipse 运行javaweb项目tomcat启动失败
- spring/springmvc/tomcat启动报错:Servlet mapping specifies an unknown servlet name taotao-search,Failed t
- Tomcat启动时Initializing Spring FrameworkServlet 'springmvc'卡住,并且不报错
- 实现tomcat启动时自动运行代码
- myeclipse里面tomcat无法启动,运行就挂起的问题及解决
- 如何配置tomcat以运行java的servlet程序?
- Tomcat启动时一闪而过,但是jdk环境变量配置正确,并使用java或者javac命令多可以运行,但是tomcat不能运行解决办法。
- tomcat:start可以启动 用debug无法运行
- solr入门之solr5.5.0源码导入eclipse运行tomcat启动项目报404的解决