in the javax.servlet.GenericServlet; class why init (ServletConfig cg) is not finalized? i mean there is another init() to over ride, but why they doesn't made init (ServletConfig cg) as final method? we can easily override this method in our class., so what is the use if we override this version of init(-) rather than over riding init()?
thanks in advance.,
eagerly waiting for your replies.,
when init() method is called there is no servlet instance is associate with this servlet class. init() is first method called to make this servlet class a real servlet. at this time there is no servletConfig object also. so you can use init(ServletConfig config) method to pass other servletsServletConfig object. if it is final then you can not do this.
pankaj vijay wrote:at this time there is no servletConfig object also. so you can use init(ServletConfig config) method to pass other servlets ServletConfig object.
I don't quite follow the logic behind this, but your code should neither call other servlet's init methods, nor pass around ServletConfig objects.
Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet.init(javax.servlet.ServletConfig).
This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).
This is what init(config) method is used, you can overwrite this method but then you will need to call the Super.init(config) method, because if you forget this then, I suppose the initialization process of your servlet will not complete properly, the servlet does not have access to the ServletConfig and the application throws a NullPointerException if you try to access it.
A convenience method which can be overridden so that there's no need to call super.init(config).
Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().
Making a method final would be necessary if there would be a huge impact if someone tried overriding the method, if you look at what code is there in the init(config) method
there is nothing that one will miss or do after overriding as there is nothing one can do in the init method with respect to different JSP API/JSP implicit objects. Nothing is available so I think they thought if it is not going to be misused why even make it final. Though it throws an NullPointer but then thats trivial, not big enough problem to make a method final.
So the best practice is to use init() method and not init(config) method and forget final for these methods.
Read about final on Wiki and there are some links on the same page that talk about why and when to use final.
Finally, have a nice day!
Regards,
Chetan Shinde
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.