Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Initialization

 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet gets initialized in following two ways
1)Invoke corresponding constructor
2)Call init()

What I want to know is when do container go for these options ie when do container use option 1 and when do container use option 2
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

They both happen as part of the servlets life cycle.
The whole life cycle is as follows

1) the servlet is loaded into the jvm
2) constructor is called
3) init(ServletConfig) is called by the container (this calls init() which you can override)
4) container calls service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException for every request it receives for the servlet
5) when the container is about to go down it calls destroy()

Mat
 
Reghu Ram Thanumalayan
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rewa Dev:
Servlet gets initialized in following two ways
1)Invoke corresponding constructor
2)Call init()


Your question can be rephrased as follows .

The servlet gets constructed in 1 and gets initialized by the container by providing the servlet config in 2. HTH.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic