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


Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
At first container loads the servlet class.Then the no-arg constructor of the servlet runs creating a servlet instance.Then the container calls the init() method on servlet instance.
My doubt is that if there is init parameter in the DD which method will be called init() or init(servlet config) or both.
And does exact similar thing happens in JSP also?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
The servlet container only calls methods that are part of the Servlet interface. But you should read the javadocs of the two GenericServlet.init methods.
ramnna jain
Ranch Hand

Joined: Mar 16, 2009
Posts: 129

Hi! Ankitt,

I am new for web application development, but I would like to share what I know:

Container stores the context-param into to ServletContext object &
then ServletConfig receives the reference to ServletContext.

If there are init-param in the DD then they are stored in the ServletConfig &
now this ServletConfig object is passed to init(ServletConfig).

init() is some thing which gives a chance to a programmer to initialize what he want for his/her servlet.
not compulsory to override.

If you override the init() then init(servletconfig) calls your init() implementation.

The init(servletconfig) is called as soon the servlet instance is created, but before service method is called.
I agree. Here's the link:
subject: init()
It's not a secret anymore!