"Servlet" uses Load-on-startup to create a thread that survives the server

Source: Internet
Author: User

Some Java EE or javaweb components require some code to be configured in the Web. Xml section, and if you look closely, you will find that most of them have load-on-startup parameters. This parameter is used in conjunction with a servlet that has only void init (ServletConfig config) to create a task that is required for a thread to run with the server. Some tasks have to start at the beginning of the server and end at the end of the server, and you can consider using the servlet's load-on-startup to complete. For example, a website requires you to check things, such as access requirements every 7,200 seconds, 2 hours to the server to feedback some information, etc., you can consider the use of this implementation.


I. BASIC OBJECTIVES

Create a servlet that outputs the current time to the console every 1 seconds, that is, the Tomcat console displays the current time every 1 seconds, and this action is in total survival with the server.



Second, the production process

1, the first site project directory structure as shown, there is no page, Under the SRC folder there is only one secondtimer.java waiting to be called by Web. XML, put Javax.servlet-api-3.1.0.jar in the Lib folder, lest some of the lower versions of Tomcat do not follow the Servlet3.0 specification, of course This example is in the lower version of the Servlets are no problem, backward-compatible, afraid of some unknown Tomcat server does not support servlet. Javax.servlet-api-3.1.0.jar you search the Internet a lot, more than 3.0 on the line, in order to avoid such a problem.



2. Web. xml as shown below, unlike my simplest servlet javaweb program in the servlet (click the Open link), the Secondtimer in the root directory is executed as soon as the server runs. The Load-on-startup parameter is the priority, the less the higher the priority, the highest priority is 0. This means that if there are more than one such servlet, then which Load-on-startup is less and which is executed first.

<?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><servlet-name>timer</servlet-name ><servlet-class>SecondTimer</servlet-class><load-on-startup>1</load-on-startup>< /servlet></web-app>


3, the original servlet is to respond to the request after the work will disappear, but the servlet initiated by Load-on-startup because only the Init method, there is no Dopost,doget method, or Dopost, Doget Merge Method Service, the servlet does not have a mapped address, so the servlet will never be destroyed.

Import java.util.*;import java.text.simpledateformat;import javax.servlet.*;import javax.servlet.http.*;p ublic Class Secondtimer extends HttpServlet {public void init (ServletConfig config) throws servletexception {Timer timer = new T Imer (); Timer.schedule (new MyTask (), 0, 1000);}} Class MyTask extends TimerTask {public void run () {System.out.println (new SimpleDateFormat ("YYYY year mm month DD Day HH:MM:SS E"). Format (New Date (System.currenttimemillis ())). ToString ());}}

As for how to output time to the console, the principle and "Java" using timer and TimerTask timed tasks (click to open the link) exactly the same, no longer repeat.

"Servlet" uses Load-on-startup to create a thread that survives the server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.