tomcat启动后让servlet一直运行
2015-06-15 18:42
525 查看
.1.web.xml 中设置servlet为<load-on-startup>1</load-on-startup>
[html] view
plaincopy
<?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方法中执行启动业务线程
[java] view
plaincopy
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 定时运行
[java] view
plaincopy
/**
*
*/
/**
* @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)
{
}
}
}
http://blog.csdn.net/baijd_ss/article/details/8489142
[html] view
plaincopy
<?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方法中执行启动业务线程
[java] view
plaincopy
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 定时运行
[java] view
plaincopy
/**
*
*/
/**
* @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)
{
}
}
}
http://blog.csdn.net/baijd_ss/article/details/8489142
相关文章推荐
- debian安装tomcat
- tomcat单机实例配置
- nginx配置tomcat与配置rewrite。
- Tomcat 安装webalizer
- eclipse中访问不了tomcat首页server Locations变灰无法编辑
- Tomcat中文乱码处理总结
- Tomcat7.0安装方法
- eclipse debugger模式下,tomcat不能重新启动
- 在URL中编码中文,Tomcat服务端接收出现乱码
- Spring4.x Tomcat JNDI 配置问题
- Eclipse 4000 中用Tomcat发布的Web项目,更改其部署路径
- 如何让项目使用myEclipse部署在tomcat的ROOT目录下
- 一劳永逸部署项目:通过tomcat加载环境变量
- 实例013 Tomcat如何制定主机访问
- Tomcat实现访问限制
- Eclipse +Tomcat配置JNDI数据源
- Netty+Tomcat热部署端口占用解决办法(转)
- Maven配置-使用Maven创建WEB项目支持Tomcat目录部署
- 图解Tomcat类加载机制
- 浏览器网址请求Tomcat服务器UML图全过程