• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what do init () method do

 
hari vallabh shukla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what intialization do init() method do in servlet and what enviroment variable it assign to servletcontext object
 
M Mohile
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 vallabh shukla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we have to override init() no-arg method & call super init() method. Then only initialization will happen.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic