This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() and init(ServletConfig) doubt?

 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is covered in the ServletsFaq.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ulf!
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea guys?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Atul Sawant
Ranch Hand
Posts: 304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Narendra Dhande
Ranch Hand
Posts: 951
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3380
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I got this! Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic