File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when init() method is called ?

 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just once, when the servlet is initialised.
 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its a first method of servlet's life cycle so it must be executed every time servlet is invoked..

regs
Prashant
 
Mac Luq
Greenhorn
Posts: 22
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Eclipse IDE Firefox Browser MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
service(), not servise().
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
Java Mac OS X Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks
and sorry for the stupid questions
 
jeya velan
Greenhorn
Posts: 9
Eclipse IDE Firefox Browser MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic