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 are closed for "What is preinitialization of a java servlet?".
Thanks for all the answers.Answers are complete and specific.These are very helpful.
This tutorial is concise and helpful for last minute revision.
Thx
“This initialization can be done in three ways.”…. 01. Lazy loading (default) 02. Using and 03. ?
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?
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?
Very good article for a new person like me to learn these things
on which basis servlet is called if there are more than one servlet in your application
thankQ…………..very nice
zero will do the same as positive number.
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
what is servlet
Nice blog….learned new things
Great Explanation. Thank you Joe, and
missing 3rd way of initialization.
Very Helpful
hi
i am new to java, I wish to generate output as pdf for some reports please can you help me
very nice..
What is third way to instantiate servlet?
I read the servlet part and you written it in such a manner that anyone can easily understand.
Thank you.
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
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?
thanx its super
Simple and Clear Explanantion Thanks Joe but Where is the third method joe?
Nice….!
it is very helpful for java learners
hello sir…
i hv a doubt ..that u have describe only 2 ways of servlet initializatn but u hv said there are three ways..???
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..?
Hi Joe,
can u give more explanation on this
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.
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
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