• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what do init () method do

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what intialization do init() method do in servlet and what enviroment variable it assign to servletcontext object
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we have to override init() no-arg method & call super init() method. Then only initialization will happen.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic