aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes init(sevletConfig) doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "init(sevletConfig) doubt" Watch "init(sevletConfig) doubt" New topic
Author

init(sevletConfig) doubt

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
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

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Jul 20, 2005
Posts: 215
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

Joined: Nov 15, 2004
Posts: 1026
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 ]
 
Don't get me started about those stupid light bulbs.
 
subject: init(sevletConfig) doubt