What is preinitialization of a java servlet?

Last modified on October 5th, 2014 by Joe.

In the java servlet life cycle, the first phase is called ‘Creation and intialization’.

The java servlet container first creates the servlet instance and then executes the init() method. This initialization can be done in three ways. The default way is that, the java servlet is initialized when the servlet is called for the first time. This type of servlet initialization is called lazy loading.
 
The other way is through the

<load-on-startup>non-zero-integer</load-on-startup>

tag using the deployment descriptor web.xml. This makes the java servlet to be loaded and initialized when the server starts. This process of loading a java servlet before receiving any request is called preloading or preinitialization of a servlet.

Servlet are loaded in the order of number(non-zero-integer) specified. That is, lower(example: 1) the load-on-startup value is loaded first and then servlet with higher values are loaded.

Example usage:

<servlet>
       <servlet-name>Servlet-URL</servlet-name>
       <servlet-class>com.javapapers.Servlet-Class</servlet-class>
       <load-on-startup>2</load-on-startup>
</servlet>

Comments on "What is preinitialization of a java servlet?"

  1. Madhuparna Mukherjee says:

    Thanks for all the answers.Answers are complete and specific.These are very helpful.

  2. satya samal says:

    This tutorial is concise and helpful for last minute revision.

  3. xd says:

    Thx

  4. dkjena4u says:

    “This initialization can be done in three ways.”…. 01. Lazy loading (default) 02. Using and 03. ?

  5. Dipraj says:

    Initialization can be done in three ways.

    1. Lazy Loading(when servlet gets its first request then instantiate.)

    2. (Non zero integet)

    [As per resin if value 0 or +ve integer then load lower value first. If the value is negative or unspecified, then the container can load the servlet at anytime during startup.]

    What is third way to instantiate servlet?

  6. yatish sharma says:

    my question is that-how can we call a servelet?
    See, I make a servelet “HelloWorld” and I want to show a message like ‘hello world’ than how I call this servelet?

    Can I instantiate a swrvelet on jsp page?

  7. Justin says:

    Very good article for a new person like me to learn these things

  8. Vishal Singh says:

    on which basis servlet is called if there are more than one servlet in your application

  9. suresh says:

    thankQ…………..very nice

  10. Anonymous says:

    zero will do the same as positive number.

  11. akbar says:

    hi joe, this is very useful information,
    thanks for this

    but my doubt is where is the third way, if you have answer please update it

    you mentioned above like “This initialization can be done in three ways”

    so asked, if there is no third way then can you please update in this answer

    thanks

  12. chimu says:

    what is servlet

  13. Tuhin says:

    Nice blog….learned new things

  14. Shashi Kanth says:

    Great Explanation. Thank you Joe, and
    missing 3rd way of initialization.

  15. Jp says:

    Very Helpful

  16. guptha says:

    hi
    i am new to java, I wish to generate output as pdf for some reports please can you help me

  17. Biswa says:

    very nice..

    What is third way to instantiate servlet?

  18. harshal says:

    I read the servlet part and you written it in such a manner that anyone can easily understand.

    Thank you.

  19. Khaleel says:

    Hi Joe,

    Nice and crispy information on basic servlet initialization. However many asked and I am also asking the same question that “what is the third way of initializing a servlet”?

    I hope there are only these two ways to initialize the servelet.

    — Khaleel

  20. Ilyas says:

    Want to call a bean method automatically when server starts.

    web.xml

    MyServlet
    MyServlet
    2

    MyServlet
    /MyServlet

    MyServlet
    public void init(ServletConfig config) throws ServletException{

    // call a bean method after x seconds. This time is controlled by java.util.Timer and TimerTask.

    }

    Is this approach correct?

  21. Anonymous says:

    thanx its super

  22. mukesh reddy says:

    Simple and Clear Explanantion Thanks Joe but Where is the third method joe?

  23. Richa says:

    Nice….!

  24. Gadadhar says:

    it is very helpful for java learners

  25. sakshi tayal says:

    hello sir…
    i hv a doubt ..that u have describe only 2 ways of servlet initializatn but u hv said there are three ways..???

  26. krishna says:

    Does it required for a load on start up servlet call url mapping is necessary..?if not how it internally happens…?if yes why should servlet itself required request…can u plz confirm on this..?

  27. sachin says:

    Hi Joe,

    can u give more explanation on this

  28. Jeetendra says:

    3rd way , as alternatively,we might want to use a ServletContextListener to do initialisation work when the container comes up. This is the ‘de facto’ standard for having a callback to do some initialisation work when the servlet container comes online e.g. we use that to read in some XML files and populate a cache.

  29. Jeetendra says:

    3rd way , as alternatively,we might want to use a ServletContextListener to do initialisation work when the container comes up. This is the ‘de facto’ standard for having a callback to do some initialisation work when the servlet container comes online e.g. we use that to read in some XML files and populate a cache.

    MyServlet

  30. Anonymous says:

    The initialization can be done in three ways
    1.When the server starts
    2,When the servlet is first requested, just before the service() method is invoked
    3.At the request of the server administrator

Comments are closed for "What is preinitialization of a java servlet?".