aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes init() method doubt? 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 "init() method doubt?" Watch "init() method doubt?" New topic
Author

init() method doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

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?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Saurabh Kumar
Ranch Hand

Joined: Aug 21, 2006
Posts: 56
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

Joined: Sep 26, 2006
Posts: 9622
    
    2

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

Joined: Aug 21, 2006
Posts: 56
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

Joined: Sep 26, 2006
Posts: 9622
    
    2

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

Joined: Aug 21, 2006
Posts: 56
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

Joined: Mar 18, 2004
Posts: 572
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

Joined: Sep 26, 2006
Posts: 9622
    
    2

"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

Joined: Mar 18, 2004
Posts: 572
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

Joined: Sep 26, 2006
Posts: 9622
    
    2

Thanks Ali!
Meeta Kate
Greenhorn

Joined: Mar 06, 2007
Posts: 2
Hi Jothi

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

Please take a look!


SCJP<br />SCWCD- Preparing
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Thanks Meeta!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init() method doubt?