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

init() and init(ServletConfig) doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Guys,

Why is that when we override init(ServletCongif), we write super.init() as the first line?? I vaguely remember but not clear. Can anyone help me with this!


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!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
This is covered in the ServletsFaq.


Ping & DNS - my free Android networking tools app
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Thanks ulf!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Got this from the faq,

"If we override the init(ServletConfig) method that takes a ServletConfig then super.init(config) should be the first statement inside that overridden method.

Because the container creates a ServletConfig instance and passes that instance to the init method of the GenericServlet where that config instance is assigned to the local private copy of that class. (private transient ServletConfig "

I want to know why actually the container creates a ServletConfig instance and passes it to the init method of the GenericServlet class?? Any specific technical reasons??
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Can I do this instead of calling super.init(config)??

I override the one that takes ServletConfig instance as parameter and instead of writing super.init(config), I do this,

this.config = config

Would the above case work??
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

The init(ServletConfig) method is from Servlet interface. It is a contract. The init() method is a convenience method which is given in GenericServlet abstract class. As the HttpServlet implements GenericServlet , this method is available for you.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Ok fine, the init(ServletConfig) method is a contract in the Servlet interface. But my point here is, I'm going to override it but still I want access to the ServletConfig instance without doing the super.init(config). Can I do this instead, this.config = config so that I get a reference to the ServletConfig without calling super.init(config)?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Any idea guys?
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Jothi Shankar Kumar Sankararaj:
Can I do this instead of calling super.init(config)??

I override the one that takes ServletConfig instance as parameter and instead of writing super.init(config), I do this,

this.config = config

Would the above case work??


Hi Jothi,

What makes you think there's a 'config' member in your servlet? Take a look at the classes you're extending (HttpServlet and GenericServlet) and you won't find any (public) member with that name. It may be true that there is one but I think it should be private, so there's no way for you to use it directly.

Anyway, try it out yourself and tell us what you find out.

Best of lucks for you.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Hey Sergio...so nice to see you back!

Jothi - I think we both are going back to basics after "trying" to read Struts. Just kidding...u rock dude!
[ June 08, 2007: Message edited by: Atul Sawant ]

Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

The default implementation of init(ServletConfig) called the init() method. If you override the init(ServletConfig) method and store the reference of config, your init() method is not called by the container automatically, as you overriding the default implementation. That is the init() method will not run.

Thanks
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Atul,

You are right...we are going to the basics. But I'm sure that the Head First did not address this issue!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

If I declare a variable that holds the config instance, then I don't have to call super.init(config) when I override init(ServletConfig). Am I right?
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3257
Read this link and it may help you understand the reason

[ UD: removed links to copyrighted material ]
[ August 03, 2007: Message edited by: Ulf Dittmer ]

500+ Java Interview Questions and Answers | Java job hunting know how & Java resumes
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Jothi,

As someone else said, though it may seem to be right for having a ServletConfig instance in your class and assigning the config object passed in the overloaded init() method to the local reference "config" may solve the purpose.

But i think as a part of contract, there should be something more what a Servlet Container does it for you with the config object, which we may not be doing it! - this is why the Spec also insists you to have the delegation to "super.config(init)" as the first line in your overloaded method.

May be, like we can get the initialized values (what we set it in web.xml file) etc. But the relation with ServletContext, must be set by the Servlet Container i think.

Does it make some sense?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9513
    
    2

Ok, I got this! Thanks
 
jQuery in Action, 2nd edition
 
subject: init() and init(ServletConfig) doubt?