permaculture playing cards
The moose likes Servlets and the fly likes what do init () method do Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "what do init () method do" Watch "what do init () method do" New topic

what do init () method do

hari vallabh shukla

Joined: Jan 31, 2013
Posts: 21
what intialization do init() method do in servlet and what enviroment variable it assign to servletcontext object
M Mohile

Joined: Sep 22, 2012
Posts: 24
Like any object in Java, Servlet is also an object.
Before init () method, the no-argument constructor of the Servlet class is called by the container but that only helps creating an object.
Servletness is granted to that object by Container by giving it ServletConfig object when its init(ServletConfig sg) method is called & always happens before the servlet can service any client requests.
init(ServletConfig sg) method helps give deployment information (like references to other sources in the Web App and database connections) which cannot be hard-coded in the Servlet (viz. Servlet initParameters).
ServletConfig object gives it servlet initParameters and also gives ServletContext object which helps it to communicate (secret-handshake between the Servlet and Web Container) throughout the Web App with the help of attributes.
The Container reads the Context initParameters from the Deployment Descriptor before Servlet initParameters and passes them to ServletContext object.
Then, servlet initParameters are read and given to the ServletConfig object which is then passed to the servlet’s init(ServletConfig sg) method and it is through ServletConfig object that we get ServletContext object.
The no-argument init ( ) method can be overridden in our Servlet if we have initialization code like getting database connection or registering ourselves with other objects. This no-argument init () method otherwise is called from the init (ServletConfig sg) method.

I guess that resolves your query...
hari vallabh shukla

Joined: Jan 31, 2013
Posts: 21
Servlet initialization has understand by me but in init () method any part of intialization of servletcontext is also done by servlet or giving information of web application to servletContext is done by web container and what type of information it give to servletContext
Priya chek
Ranch Hand

Joined: Feb 12, 2013
Posts: 51
we have to override init() no-arg method & call super init() method. Then only initialization will happen.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

What does the servlet's init() method do? It executes the code you put into that method. That's all. It doesn't do anything else besides that.

In most systems, the majority of init() methods don't do anything at all.

Perhaps you are confusing the init() method with what the servlet container does when it initializes a servlet? There are several things which the container does at that point, and calling the init() method is just one of them.
I agree. Here's the link:
subject: what do init () method do
It's not a secret anymore!