Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
Author

what do init () method do

hari shukla
Greenhorn

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
Greenhorn

Joined: Sep 22, 2012
Posts: 17
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.
Note:
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 shukla
Greenhorn

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: 41
we have to override init() no-arg method & call super init() method. Then only initialization will happen.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

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: http://aspose.com/file-tools
 
subject: what do init () method do
 
Similar Threads
Constructor inheritence
Why Init() method
getting exception while loading servlet
purpose of init method
Can't get the result of factorial method