• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet and Constructor

 
umasankar puranam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its obvious that a servlet is a class. Can we write a constructor in a class which extends from HttpServlet? Please answer
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uma,

Thats a good question...Good Going....


BYe
 
Richard Bradford
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not.
All classes need a constructor. If you don't write one a default constructor will be provided.
 
Santosh Jagtap
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes you can definately write constructor in ure servlet class it will be executed when your servlet gets loaded.


regards
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah you can. but again its mendatory to ask why?? why you want to do that??

please dont say tht you are going to instantiate your servet inside some of your normal java class. although you can instantiate your servlet with a default constructor. and if it is like that it is very bad idea.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
therefore remember that the only constructor that may make any sense to create is a no-argument constructor as that's the only one that the servlet engine will expect there to be and thus call.

But of course the correct place for servlet initialisation code is inside the servlet's init method, not the constructor!

And please don't think to do such things as opening global database connections and files in servlets!
At most you could use it to load some static data like global constants but a static initialisation block is probably more suitable to that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic