my dog learned polymorphism*
The moose likes Servlets and the fly likes init method of servlets 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 "init method of servlets" Watch "init method of servlets" New topic
Author

init method of servlets

sushant prabhu
Ranch Hand

Joined: Mar 22, 2001
Posts: 66
when we override the init method of servlet
we give a call to super.init(ServletConfig).
can somebody please elaborate why do we need to do this.
What actually happens when we call the super.init method
I even want to know why do we need to use a servletconfig
object to store the init parameters and access these parameters
by using the getServletConfig method in our service method.

Sushant Prabhu<br />SCJP2<br />SCWCD<br />OCA9i
asheet anand
Ranch Hand

Joined: Nov 15, 2000
Posts: 83
hi,
very intresting ?.Infact i also asked the same ? some time back
about 2 months or so and i also got the answer but you also
have to worked for it.just see the reply for my ? but for that
you have to search the ?.good luck.
asheet
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I dont override init in any of my servlets. I know there is good reason to sometimes. Someone should have a helful answer but it is slow here on weekends.


SCJP
Visit my download page
Kevin Mukhar
Ranch Hand

Joined: Nov 28, 2000
Posts: 83
In your deployment descriptor, there is a section for servlet parameters. When the servlet container loads the servlet, it reads those parameters and stores them in the ServletConfig object. You need a ServletConfig object because that is the object required by the Specification for the servlet container to store and pass init parameters to the servlet.
However, you don't need to call getServletConfig() to get a ServletConfig object. You can call all the ServletConfig methods directly without a ServletConfig object. How is this possible?
If you look at the ServletConfig class, you'll see that it's really just an interface. The ServletConfig object you get in init() is an object that implements ServletConfig. Also, if you look at the GenericServlet class, you'll see that GenericServlet also implements ServletConfig!!
Why do both ServletConfig AND GenericServlet implement this interface?
The interface ServletConfig provides methods for easily retrieving the init parameters for the servlet. Since all servlets have GenericServlet as a parent class, every servlet is guaranteed to have the methods for getting servlet parameters.
When you call super.init(ServletConfig) the GenericServlet class stores the reference to the ServletConfig object. This allows you to call getInitParameter(String) directly in your servlet without a specific ServletConfig reference. That is, you don't need to write
ServletConfig sc = getServletConfig();
String param = sc.getInitParameter(name);
you can just write:
String param = getInitParameter(name);
The getInitParameter() call goes to the GenericServlet, where GenericServlet delegates the call to the ServletConfig object that was stored earlier.
If you do not call super.init(ServletConfig), the config object is not stored, so any calls to ServletConfig methods would fail. Of course, if your servlet has not init parameters, and you never need any of the other methods of the ServletConfig interface, then it's safe to not call the super.init(ServletConfig) method.
Originally posted by sushant prabhu:
when we override the init method of servlet
we give a call to super.init(ServletConfig).
can somebody please elaborate why do we need to do this.
What actually happens when we call the super.init method
I even want to know why do we need to use a servletconfig
object to store the init parameters and access these parameters
by using the getServletConfig method in our service method.


[This message has been edited by Kevin Mukhar (edited March 27, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init method of servlets