• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

init() method doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I have the following doubt?? Why there is an overloaded version of the init() method in the GenericServlet abstract class?? Any reasons for it?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
init(ServletConfig) is called by container and saves ServletConfig object for you.
init() is a convenience method for developer for providing servlet initialization.
However,you can override init(ServletConfig) but in that case you must include call to super.init(ServletConfig).

Hope this helps.

Thanks and regards,
Saurabh
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However,you can override init(ServletConfig) but in that case you must include call to super.init(ServletConfig).

Any reasons why?? I can just override init(ServletConfig) but why do I have to provide a call to super.init(ServletConfig)??
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because until container calls init(ServletConfig), you won't get ServletConfig object which you need/use in servlet initialization like reading servlet initialization parameters from web.xml.

Thanks and regards,
Saurabh
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saurabh,

Why do I have to do super.init(ServletConfig)?? If I override this in my servlet, it is more than enough, I have the ServletConfig...Am I right?
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why do I have to do super.init(ServletConfig)?? If I override this in my servlet, it is more than enough, I have the ServletConfig...Am I right?


no, you do "super.init(ServletConfig)" to get ServletConfig object.

That's why there is other method for your convenience: init().

Thanks and regards,
Saurabh
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jothi,

You are right you have the ServletConfig object when you override init(ServletConfig) because you can save it for further use.

But by overriding init(ServletConfig) you can't use getServletConfig() method and also you can't use getServletContext() method to get the servlet context instead you have to use request.getSession().getServletContext() to get the servlet context. Similarly you can't use getInitParameter("param") to get the initialization parameters.

Hope it clarifies
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"But by overriding init(ServletConfig) you can't use getServletConfig() method and also you can't use getServletContext() method to get the servlet context instead you have to use request.getSession().getServletContext() to get the servlet context. Similarly you can't use getInitParameter("param") to get the initialization parameters."

WHY??
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
init(ServletConfig) is defined in GenericServlet class which actually does multiple things for you

- It calls the init() method from it which you override in your servlet
- It saves ServletConfig object and ServletContext object for you in instance variables and you can get them using getServletConfig() and getServletContext() methods

- Similarly GenericServlet provides you a utility method to get init params which is getInitParams(param)
[ March 06, 2007: Message edited by: Ali Gohar ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ali!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi

This is very well explained here[URL=http://http://faq.javaranch.com/view?ServletsFaq#4]

Please take a look!
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Meeta!
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic