Solved

servlet loading

Posted on 2014-03-26
1
496 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now