• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor of a Servlet ?

 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I have the following doubt :
As I understand, the servlet is instantiated by the servlet container, when the first request is sent by a user (client).
Then only its service method is called.
But then where is the constructor of the servlet located ? I mean how does the container instantiates the servlet object ?
Thanks in advance
Mandar
 
Osama Hasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container uses the default constructor to instantiate the servlet.You can define the constructor if you want.
 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Osama,
Thanks for the answer, but where is that constructor ?
Mandar
 
Osama Hasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default constructor is created implicitly when no constructor is provided.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the JavaDocs for GenericServlet and HttpServlet in the servlet API. You will find empty no-args constructors. The only thing a servlet constructor has to do is initialize memory for the instance variables. This is similar to the Applet API.
Thats because both servlets and applets are held in a "container" that provides many functions, including instance construction. The init method is where you are expected to do custom initialization using the servlet context.
Just one of the many differences you have to get used to when programming servlets.
Bill
 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks Osama and William.
That clears my doubts about the Servlet Constructor.
Will be back with more doubts, as I go on..

Mandar
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!