| Author |
when init() method is called ?
|
munjal upadhyay
Ranch Hand
Joined: Sep 18, 2010
Posts: 69
|
|
I had read that the init() method is called only once ..
but , Is the init() method is called once for every request for that servlet ?
or it is called only at once when the first time the servlet is called by he container , and then not called for eny requests.. ?
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
|
Just once, when the servlet is initialised.
|
 |
munjal upadhyay
Ranch Hand
Joined: Sep 18, 2010
Posts: 69
|
|
Matthew Brown wrote:Just once, when the servlet is initialised.
but servlet is initilized each and every time , the requests comes.
so its indirectly mean that init() must be called for each and every request that calls the Servlet.
|
 |
Prashant Chindhade
Ranch Hand
Joined: Jan 19, 2011
Posts: 77
|
|
its a first method of servlet's life cycle so it must be executed every time servlet is invoked..
regs
Prashant
|
 |
Mac Luq
Greenhorn
Joined: Oct 02, 2009
Posts: 21
|
|
munjal upadhyay wrote:
Matthew Brown wrote:Just once, when the servlet is initialised.
but servlet is initilized each and every time , the requests comes.
so its indirectly mean that init() must be called for each and every request that calls the Servlet.
Hi Munjal.
Here is where you're wrong. The servlet is initialized just once, but for every new request a new thread is created or allocated from a pool to invoke that servlet instance on the appropriate method.
The HttpRequest and HttpResponse objects will be new for each new request, and the thread, but not the servlet instance.
|
 |
Mac Luq
Greenhorn
Joined: Oct 02, 2009
Posts: 21
|
|
prashant chindhade wrote:its a first method of servlet's life cycle so it must be executed every time servlet is invoked..
regs
Prashant
No, that's wrong, see my previous answer.
|
 |
jeya velan
Greenhorn
Joined: Feb 07, 2011
Posts: 9
|
|
When the container receives the first request, It will create an instance of the servlet.
Then the container will call the init method which does the intializtion process.
Then it starts to service the request.
Then for the subsequent requests, A thread will be created per request which executes the service method in its own stack.
At any time there will be only one instance of a particular servlet in the memory per JVM.
If you preload your servlet, than creating and initializing will be completed at the server start up time itself.
|
 |
munjal upadhyay
Ranch Hand
Joined: Sep 18, 2010
Posts: 69
|
|
jeya velan wrote:When the container receives the first request, It will create an instance of the servlet.
Then it will call the init method which does the intializtion process.
Then it starts to service the request.
Then for the subsequent requests, A thread will be created per request which executes the service method in its own stack.
At any time there will be only one instance of a particular servlet in the memory per JVM.
If you preload your servlet, than creating and initializing will be completed at the server start up time itself.
the initialization and init() is different
initilization :- the constructor part of the servlet class is done , and the ordinary object of that class is created
init() :- the ordinary object --> servlet object , and servlet object has all the writed like accessing the container data etc...
I had read this in headFirst servlet and Jsp .
so
your statement :- Then it will call the init method which does the intializtion process.
is not conviscing.
will you please please rewite it
thanks in advanced.
|
 |
Kumar Raja
Ranch Hand
Joined: Mar 18, 2010
Posts: 457
|
|
Hi Munjal,
Based on the container implementation, the container would decide how many instances of servlet would be instantiated upfront to cater the requests. This is container implementation. So, let us assume that a trivial container would decide to have only one instance of the servlet at any point of time. So, when the first requests comes in , again based on your configuration "load-on-startup" either container would pre-instantiate your servlet instance at the container startup or may defer the instantiation process when the first request comes in. In either case, the constructor is called and container calls the init() method. Servlet instance instantiation is different and calling init() is different. usually we will keep the logic like acquiring any DB connections, any other resources etc are handled in init() method. With the completion of init(), the servlet is ready to handle any request. Now, upong any subsequent requests, container would create a thread and invoke the service() method which in turn calls your http related methods, if you have extended HttpServlet. So for every request, servlet instance is not created and init() is not called. The only situation for every request, Servlet instance is called is when you choose to have that way, by implementing SingleThreadModel, which is not recommended and also deprecated.
Similarly, destroy() method is called only once, when container chooses to destroy the servlet and will not handle any requests any more.
Hope this is clear now.
|
Regards
KumarRaja
|
 |
munjal upadhyay
Ranch Hand
Joined: Sep 18, 2010
Posts: 69
|
|
Mac Luq wrote:
munjal upadhyay wrote:
Matthew Brown wrote:Just once, when the servlet is initialised.
but servlet is initilized each and every time , the requests comes.
so its indirectly mean that init() must be called for each and every request that calls the Servlet.
Hi Munjal.
Here is where you're wrong. The servlet is initialized just once, but for every new request a new thread is created or allocated from a pool to invoke that servlet instance on the appropriate method.
The HttpRequest and HttpResponse objects will be new for each new request, and the thread, but not the servlet instance.
I got your answer , but
first request
---------------->container--> loading,initializing(when deployed)--> instance of servlet(HttpRequest,HttpResponse)--> Thread(HttpRequest,HttpResponse)--> init()--> servise()--> destroy--> deleteThread
^
subsequent requests |
----------------------------------------------------------------------------
I got this from the searching
and its not fit with your one.
whats is wrong with this ???
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
Your assumptions are false.
The init() method is called once, when the servlet is first put into service.
The destroy() method is called once, when the servlet is taken out of service.
Neither is called on every request.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
munjal upadhyay
Ranch Hand
Joined: Sep 18, 2010
Posts: 69
|
|
Bear Bibeault wrote:Your assumptions are false.
The init() method is called once, when the servlet is first put into service.
The destroy() method is called once, when the servlet is taken out of service.
Neither is called on every request.
| |
|first request |subsequent
| |request
| |
container |
| |
| |
loading,initializing(when deployed) |
| |
| |
init() |
| |
|<--------------------------------------
instance of servlet(HttpRequest,HttpResponse)
|
|
Thread(HttpRequest,HttpResponse)
|
|
servise()
|
|
destroy()
|
|
deleteThread or send back to Container to reuse.
now perfect ???
|
 |
munjal upadhyay
Ranch Hand
Joined: Sep 18, 2010
Posts: 69
|
|
munjal upadhyay wrote:
Bear Bibeault wrote:Your assumptions are false.
The init() method is called once, when the servlet is first put into service.
The destroy() method is called once, when the servlet is taken out of service.
Neither is called on every request.
| |
|first request |subsequent
| |request
| |
container |
| |
| |
loading,initializing(when deployed) |
| |
| |
init() |
| |
|<--------------------------------------
instance of servlet(HttpRequest,HttpResponse)
|
|
Thread(HttpRequest,HttpResponse)
|
|
servise()
|
|
destroy()
|
|
deleteThread or send back to Container to reuse.
now perfect ???
why this stupid compiler escaping the white spaces???
|
 |
Kumar Raja
Ranch Hand
Joined: Mar 18, 2010
Posts: 457
|
|
Again,
No constructor calling or init() method calling , after it has been called once, for any subsequent requests. What made you think, that container would instantiate a servlet instance for every request. We are not using SingleThreadModel anymore.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
munjal upadhyay wrote:why this stupid compiler escaping the white spaces???
It's HTML. HTML does that. Use the code tags if you want to keep the layout fixed.
|
 |
munjal upadhyay
Ranch Hand
Joined: Sep 18, 2010
Posts: 69
|
|
Kumar Raja wrote:Again,
No constructor calling or init() method calling , after it has been called once, for any subsequent requests. What made you think, that container would instantiate a servlet instance for every request. We are not using SingleThreadModel anymore.
so you going to say that
when there is a first request , the loading , initiliazing + calling the init() , is done and then the following step
1) create instance of servlet(HttpRequest,HttpResponse)
2) create Thread(HttpRequest,HttpResponse)
3) call servise()
4) deleteThread
will be conducted
and then for the subsequent requests only (1,2,3,4) steps be conducted
and if the container don't want to take that Servlet's request , then only destroy() is called
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
|
service(), not servise().
|
 |
Kumar Raja
Ranch Hand
Joined: Mar 18, 2010
Posts: 457
|
|
For the very first request (and not for subsequent requests or if load-on-startup, then when the container is started
1) Call servlet constructor
2) Init()
at this point, Servlet is ready to handle the requests, so for the first request and for the subsequent requests
3) create a thread and call Service
Steps 3 repeat for all requests.
Now if container is going down,
4) destroy method
Hope this is clear now.
|
 |
munjal upadhyay
Ranch Hand
Joined: Sep 18, 2010
Posts: 69
|
|
thanks
and sorry for the stupid questions
|
 |
jeya velan
Greenhorn
Joined: Feb 07, 2011
Posts: 9
|
|
munjal upadhyay wrote:
jeya velan wrote:When the container receives the first request, It will create an instance of the servlet.
Then it will call the init method which does the intializtion process.
Then it starts to service the request.
Then for the subsequent requests, A thread will be created per request which executes the service method in its own stack.
At any time there will be only one instance of a particular servlet in the memory per JVM.
If you preload your servlet, than creating and initializing will be completed at the server start up time itself.
the initialization and init() is different
initilization :- the constructor part of the servlet class is done , and the ordinary object of that class is created
init() :- the ordinary object --> servlet object , and servlet object has all the writed like accessing the container data etc...
I had read this in headFirst servlet and Jsp .
so
your statement :- Then it will call the init method which does the intializtion process.
is not conviscing.
will you please please rewite it
thanks in advanced.
You can reword it to The container will call the init method which transforms the POJO to real servlet
|
 |
 |
|
|
subject: when init() method is called ?
|
|
|