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