Can load-on-startup in web.xml be used to load an arbitrary class on startup?

后端 未结 4 818
旧时难觅i
旧时难觅i 2020-12-28 17:45

How can I load an arbitrary class on startup in Tomcat? I saw load-on-startup tag for web.xml file, but can I use it and how should I implement my

4条回答
  •  清酒与你
    2020-12-28 18:32

    The element load-on-startup indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the Web application. The element content of this element must be an integer indicating the order in which the servlet should be loaded.In other words, container loads the servlets in ascending integer value. The 0 value will be loaded first then 1, 2, 3 and so on.

    Let's try to understand it by the example given below:

    web.xml

      
     ....  
      //=====================servlet 1==============
        
       servlet1  
       com.javatpoint.FirstServlet  
       0  //value given 0(zero)
        
    
      //=====================servlet 2==============
        
       servlet2  
       com.javatpoint.SecondServlet  
       1   //value given 1(one)  
        
    
     ...  
      
    

    There are defined 2 servlets, both servlets will be loaded at the time of project deployment or server start. But, servlet1 will be loaded first then servlet2.

    Passing negative value : If you pass the negative value, servlet will be loaded at request time, at first request.

提交回复
热议问题