"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??
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.
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??
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.
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 ]
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.
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?