Can a init(ServletConfig config) method be overrided in servlets?

Questions by javavikas

Showing Answers 1 - 6 of 6 Answers

Sorry Brijesh, I beg to differ from you. Over Riding an init() method is very well possible because u might need some extra resource to be initialized when the application is invoked.

  Was this answer useful?  Yes

javamatrix

  • Nov 8th, 2005
 

The init() method of servlet can be overridden. Remember the init() method is overloaded in the GenericServlet class. But the difference lies in which init method are u calling..

if you override the init(ServletConfig config) method, then you will have to call explicitly super.init(config) inside your code...

if you override the init() method, then u dont have to call the super.init() method explicitly..

  Was this answer useful?  Yes

javamatrix

  • Nov 8th, 2005
 

The init() method of servlet can be overridden. Remember the init() method is overloaded in the GenericServlet class. But the difference lies in which init method are u calling..

if you override the init(ServletConfig config) method, then you will have to call explicitly super.init(config) inside your code...

if you override the init() method, then u dont have to call the super.init() method explicitly..

  Was this answer useful?  Yes

Bhupender Giri

  • Nov 9th, 2005
 

The init() method of servlet can be overridden. Remember the init() method is overloaded in the GenericServlet class. But the difference lies in which init method are u calling..

if you override the init(ServletConfig config) method, then you will have to call explicitly super.init(config) inside your code...

  Was this answer useful?  Yes

vperi

  • Mar 19th, 2009
 

Yes. We can override.

The init(ServletConfig) method is defined in the Servlet
interface. It has been implemented by the GenericServlet class. The
implementation of this init method in the GenericServlet just stores the
ServletConfig object in some private variable so that when you use the method "getServletConfig()",
it gives you the ServletConfig object.


If you override this init(ServletConfig) method but do not call "super.init(config)",
then you need to take care of storing the ServletConfig object in your own
member variable so that you can use it later in your servlet. This means you
cannot retrieve it using "getServletConfig()" method.


As an alternative, you can simply override the "init()" method (without any
parameters). In this case, you need not call "super.init(config)". If in this
init method, you need the ServletConfig object, you can get it via "getServletConfig()"
method. If you define this version of the init method, the GenericServlet's
init(config) method will take care of calling your parameter-less init method.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions