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..
The Art Of Life Is To Know When To Be Useless And When To Be Useful - CHUANG TZU
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:
“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
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 ]
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.