• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Just a simple servlet question

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic