load-on-startup in Servlet

<servlet>
          <servlet-name>TimeServlet</servlet-name>
          <servlet-class>TimeServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
     </servlet>

     <servlet-mapping>
          <servlet-name>TimeServlet</servlet-name>
          <url-pattern>/servlet/TestServlet</url-pattern>
     </servlet-mapping>
The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive 128 integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.
If load-on-startup is greater than zero then when the container starts it will start that servlet in ascending order of the load on startup value you put there (ie 1 then 2 then 5 then 10 and so on).
Servlet Life Cycle
The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
  1. If an instance of the servlet does not exist, the Web container
    1.1. Loads the servlet class.
    1.2. Creates an instance of the servlet class.
    1.3. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
  2. Invokes the service method, passing a request and response object. Service methods are discussed in the section Writing Service Methods.
yes it can have same value....the reason for giving numbers to load-on-startup is to define a sequence for server to load all the servlet. servlet with 0 load-on-startup value will load first and servlet with value 1 will load after that.
if two servlets will have the same value for load-on-startup than it will be loaded how they are declared in the web.xml from top to bottom. the servlet which comes first in the web.xml will be loaded first and the other will be loaded after that.
  1. import java.io.IOException;  
  2. import java.util.Timer;  
  3.   
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. public class TestServlet extends HttpServlet   
  10. {     
  11.     private static final long serialVersionUID = 1L;  
  12.     public TestServlet()   
  13.     {  
  14.         super();  
  15.     }  
  16.       
  17.     public void init() throws ServletException   
  18.     {  
  19.         Timer timer = new Timer();  
  20.         timer.schedule(new TestTask(), 10002000);
  21.         while (true){  
  22.             try{  
  23.                 int ch = System.in.read();  
  24.                 if (ch - 'c' == 0){  
  25.                     timer.cancel(); 
  26.                 }
  27.             } catch (IOException e){  
  28.                 e.printStackTrace();  
  29.             }  
  30.         }  
  31.     }  
  32.   
  33.     class TestTask extends java.util.TimerTask{  
  34.         public void run(){  
  35.             System.out.println("Test!");  
  36.         }  
  37.     }  
  38.       
  39.     public void destroy(){  
  40.         super.destroy();   
  41.     }  
  42.        
  43.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  44.             throws ServletException, IOException   
  45.             {   }  
  46.   
  47.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  48.             throws ServletException, IOException   
  49.             {    }  

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE