This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes A web application query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "A web application query" Watch "A web application query" New topic
Author

A web application query

Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
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


Regards,
Jignesh

The Art Of Life Is To Know When To Be Useless And When To Be Useful - CHUANG TZU
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
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>


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Thanks for the reply
Thanks for this explaination
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
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 ]

Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A web application query