Solved

servlet loading

Posted on 2014-03-26
1
519 Views
Last Modified: 2014-04-24
Hi,

I was reading as below(http://www.javatpoint.com/load-on-startup)

    <servlet>  
       <servlet-name>servlet1</servlet-name>  
       <servlet-class>com.javatpoint.FirstServlet</servlet-class>  
       <load-on-startup>0</load-on-startup>  
      </servlet>  
     
      <servlet>  
       <servlet-name>servlet2</servlet-name>  
       <servlet-class>com.javatpoint.SecondServlet</servlet-class>  
       <load-on-startup>1</load-on-startup>  
      </servlet>  
     
     ...  
    </web-app>  

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.


I have not clearly understood why servlet1 loaded then servlet2. What it means by loading one servlet after other servlet.

also what it mean by servlet loaded at request time for negative value.

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
1 Comment
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39957812
I have not clearly understood why servlet1 loaded then servlet2.
Because servlet1 has a LOWER <load-on-startup> value (0) defined in the web.xml than load-on-startup value for servlet2 (1), that is why servlet1 will be loaded first, and then servlet2.

What it means by loading one servlet after other servlet.
Or in general you may ask, "What does it mean to load a servlet?"...

Well, the servlet container does a number of things in order to "load" a servlet. First it creates an instance of the servlet class, so something like....     Servlet servlet1 = new com.javatpoint.FirstServlet();     then once it has an instance (object) of your servlet class it call the init method on it, like....     servlet1.init(config);       where 'config' is some information that the container passes to your servlet. Once that is done, your servlet is "loaded"

So to go back to your original question, the order of "loading" just controls the order in which the above process happens to each configured servlet. This may be useful to control because, for example, servlet2 may depend on things done in the loading of servlet1, and so the web.xml is configured to make sure the servlet1 loads first.

what it mean by servlet loaded at request time
So, lastly, this may now be obvious, but the negative value for <load-on-startup> just means that instead of the servlet container doing all the above when the servlet container starts up, it may "defer" this process to a later time. For example, the servlet container might not "load" the servlet straight away, but at a later point in time the servlet container receives a request that needs to be fulfilled by that servlet, so it "loads" the servlet (ie. does all the above steps) at that point and then immediately passes the request to the newly loaded servlet.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 54
servlet example 17 32
jboss wildfly 10.1 10 94
Adding multiple JVM environments to RedHat 6 7 23
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question