File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes why super.init() is used?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "why super.init() is used??" Watch "why super.init() is used??" New topic

why super.init() is used??

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 580

1)why "super.init(config)" is used in servlet ???.
2) what is the use of this?
3) in every servlet ,this should be used??

Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi jacob,

First, the container call the default constructor of your servlet class, and after that call the init(ServletConfig config), this method is in GenericServlet class. Normally you extends the HttpServlet, and HttpServlet extends from GenericServlet class.

If you override the init(ServletConfig config) in your class that extends HttpServlet you must call super.init(Servetconfig config), without this call to super, the code inside of your parent class (GenericServlet) don’t run and you can have problems. I don’t know what happens exactly in the GenericServlet init(ServletConfig), but in the init(ServletConfig) in GenericServlet class the servlet do a initialized job whit ServletConfig and ServletContext objects.

Whit the ServletConfig object, the container can pass information to the servlet class, for example the init parameters in the deployment descriptor, and a reference to ServletContext. Whit the ServletContext object the servlet can communicate with the container and, for example, can have access to log files.

It is better override the init() (without arguments) method in your servlet class. The init(ServletConfig config) in GenericServlet call the init() method when finish.
If you need a reference to ServletConfig object in your init() overridden method, you can use getServletConfig() to get a reference to it.

I agree. Here's the link:
subject: why super.init() is used??
It's not a secret anymore!