• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Just a simple servlet question  RSS feed

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Can someone please tell me the answer to this question:
Do we have a constructor in servlet ? Can we explictly provide a constructor?
Thanks
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so you know: There is a separate forum for Servlet/JSP questions. To answer your question: No, you do not provide a constructor for a Servlet. If you have something that has to be done only once, override the init() method. Otherwise, you provide all the code necessary by overriding the doPost() or doGet() methods.
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. A servlet must have a no-argument constructor, either one you write yourself, or the one the compiler will insert automatically if there's no other constructor. If you supply one, it will be used.

But of course there's the "init(ServletConfig config)" method. Generally, it's better to do your initialization in your implementation of that.
 
Jas Oberai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Friends,
But after reading this i'm a bit confused :yes or no


No, you do not provide a constructor for a Servlet




Yes. A servlet must have a no-argument constructor, either
one you write yourself, or the one the compiler will insert automatically if there's no other constructor.



i guess,the default constructor is always there...rite
[ July 15, 2005: Message edited by: jas oberai ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you start working with J2EE, you have a delicate balance between the specification (the API) and the actual implementation, which is provided by a specific application server. When you write a servlet, it is instantiated by the application server, not you. It is therefore not a good practice to override the default constructor, because you don't know what state other objects will be in when it is instantiated. The recommended practice is to place once-only code in the servlet's init() method and not override the constructor.

In terms of pure Java, though, Ernest is right: there is always a constructor, either provided by you or the compiler.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!