• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() method doubt?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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 Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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 Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ali!
 
Meeta Kate
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Meeta!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic