This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Why is ServletConfig obj passed to init() of GenericServlet when the class implements the interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why is ServletConfig obj passed to init() of GenericServlet when the class implements the interface?" Watch "Why is ServletConfig obj passed to init() of GenericServlet when the class implements the interface?" New topic
Author

Why is ServletConfig obj passed to init() of GenericServlet when the class implements the interface?

Rajkumar Masanaiyan
Greenhorn

Joined: Sep 17, 2011
Posts: 24
GenericServlet implements ServletConfig interface which means all the interface functions can be invoked from init() function of GenericServlet. Given this context, why does the Servlet container sends ServletConfig object to init() method? I would also like to know if the ServletConfig object that is passed to GenericServlet.init(ServletConfig) different from GenericServlet object.

I know that GenericServlet is abstract but the above questions also apply to HTTPServlet.

Regards,
Ravi
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
That is the only way the new servlet instance can get the correct version of the object which the server actually uses.

Bill
Rajkumar Masanaiyan
Greenhorn

Joined: Sep 17, 2011
Posts: 24
William Brogden wrote:That is the only way the new servlet instance can get the correct version of the object which the server actually uses.

Bill


Bill: Would you please explain how GenericServlet gets the object version? Would you also explain how this object version is important to Servlet lifecycle?

--Ravi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

By "version" he meant instance.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
As Bear said, I should have used "instance" - as in the reference to the instance of ServletConfig passed in the

javax.servlet.Servlet init(ServletConfig s) method.

GenericServlet adds the convenience method init() with no ServletConfig - if you only implement that, the init(ServletConfig ) method in GenericServlet will be called and it will call your init(). See the javax.servlet.GenericServlet JavaDocs.

Bill
Rajkumar Masanaiyan
Greenhorn

Joined: Sep 17, 2011
Posts: 24
Bear Bibeault wrote:By "version" he meant instance.


Thank you Bear. I also learnt that GenericServlet implements ServletConfig interface methods by delegating to ServletConfig object passed by Servlet Container. That means getServletConfig().getInitParameterNames() is the same as getInitParameterNames().

I also got to know that originally GenericServlet didn't implement ServletConfig but later it started implementing for convenience. Do you know why?

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why is ServletConfig obj passed to init() of GenericServlet when the class implements the interface?
 
Similar Threads
about GenericServlet
Servlets
About init(ServletConfig)/Intialization
init(servletconfig)
init method of servlets