Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A web application query

 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

what I would like to ask is that in a web-application if I have many servlets & in one of them i have overrided init() method & in all other only the doGet() & doPost() are defined then will the container first invoke the servlet with a init() method automatically when the application is deployed first time.

Please elaborate on this thing..

Regards,
Jignesh
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jignesh,

Simply overriding the init() method does not force the container to do anything. To have a container run the servlet's initalization code on deployment, add the following element into the appropriate <servlet> element in your web.xml:

<load-on-startup>1</load-on-startup>
 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply
Thanks for this explaination
 
Sharad Agarwal
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Bourdeaux:
Simply overriding the init() method does not force the container to do anything.


Just adding a little more clarity to what Paul has already shared. Simply overriding the init() method does not force the container to do anything at deployment time. If you do not have the 'load-on-startup' parameter set up, the container may choose to delay loading the servlet until the first request is made to it. At the point, the container will call the init() method. The servlet lifecycle guarantees that the init() method will be invoked before the first request is served.

From the wording of your query, it seems to me that you have some code in the init() method of one servlet that you want to be invoked before any of the other servlets are loaded. That can be achieved by setting the 'load-on-startup' as specified by Paul. Just ensure that none of the other servlets have a 'load-on-startup' less than or equal to 1.
[ July 13, 2005: Message edited by: Sharad Agarwal ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The init method is called when that particular servlet is first instantiated. That will likely be when it is first addressed over HTTP. You can force that to happen immediately after the server is started with the load-on-startup described above.
The init method works only for the servlet it's defined in, not for any of the other servlets.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic