Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init(sevletConfig) doubt

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I override the init(sevletConfig) method that takes a servletConfig
as argument then why should I call

super.init(sevletConfig).


The purpose of overriding is to give subclass specific features. Then
why should I pass a reference of the ServletConfig to the super class
(GenericServlet) init method.
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There is a convenience method init() in GenericServlet (note it takes no parameters) that you should override if you want to provide 'subclass specific features', and not override the init(SevletConfig) method.

In my version of Tomcat 5.0.28 (I Think) this is all that the init(ServletConfig) method does. This may not be the same on all versions of Tomcat or on all web containers.



HTH

Mat
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think u didn't understand my question.

I know there is a convenience init() method thats takes no argument which we are supposed to override.


But I want to know why we should call super.init(ServletConfig) if we override the init() method that take a ServletConfig instance.

 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reasons why

1)
Different containers may be doing different things in the init(ServletConfig) method. Therefore if you are possibly going to change the container that your application runs on, to ensure that the container does everything it needs to do, you should call super.init(ServletConfig).

2)
In Tomcat, at least the version I am running, ServletConfig is defined in GenericServlet as 'private transient ServletConfig config;' this means that you CAN'T do everything that the super.init(ServletConfig) does.
If you call getServletConfig() in a servlet that has overriden the init(ServletConfig) method and not called super.init(ServletConfig) you will get null.

3)
It is generally bad practice, init() is provided for this very reason. Therefore others maintaing your code will probably not be thinking that they can't call getServletConfig(), or that something else may be broken because you overrode init(ServletConfig) rather than init().

Mat
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation Mat Williams.


public void init(ServletConfig config)
throws ServletException
{
this.config = config;
init();
}

In the above code


this.config = config;


is the prime reason why I am getting a NullPointerException.

To avoid this we need a pass a instance of ServletConfig which we are getting from the container.



Wow I understood what happens behind the scenes.
[ August 08, 2005: Message edited by: vishnu prakash ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic