This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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: 42959
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!