wood burning stoves
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in Servlets" Watch "Doubt in Servlets" New topic

Doubt in Servlets

raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169

I have following doubts.

When the servlet gets initalized the container willcall init(ServletConfig) to pass the configuartion info to the servlet; but what is the use of init() when will it be called.

One More Question.
when the request is submitted the request goes to service method and based on the request methods doGet or doPost will be called is that correct.

rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
A servlet's init(ServletConfig) method is called by the server immediately after the server constructs the servlet's instance. Depending on the server and its configuration, this can be at any of these times:

1.When the server starts
2.When the servlet is first requested, just before the service() method is invoked
3.At the request of the server administrator

In any case, init() is guaranteed to be called before the servlet handles its first request. The init() takes an object and gives it its servletness (by giving the servlet config).

raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169

Thanks,But this is with respect to init(ServletConfig).what about init()?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
The javadocs of the GenericServlet.init() method mention when it will be called. Note that that method is not part of the Servlet interface, so the servlet container knows nothing about it - it is particular to the GenericServlet class.
Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
up to my knowledge

init(ServletConfig) is defined in javax.servlet.Servlet interface.as this is implemeted by GenericServlet class ,the method is implemented in that class . this method is mainly used to save ((we have a ServletConfig instance declared in GenericServlet class)) the ServletConfig object particular to the invoked servlet.after it saved the ServletConfig object it delgates to init() method which is a convenience method to be overloaded by the invoking servlet. we have getServletConfig() method to get ServletConfig object. normally we call getServletConfig() method in our servelt to get ServletConfig object.

the main purpose of init() method is to be overloaded by the invoked servlet.

coming to your second question. your opinion is absolutely right.

thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
I agree. Here's the link: http://aspose.com/file-tools
subject: Doubt in Servlets
It's not a secret anymore!