Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Contructors in Servlets

 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do we have a Constructors in java ? if Yes , Why do we use init to Initialize the Servlet.??
 
kartik hedau
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests. Why not use a constructor instead? Well, in JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments. So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init().
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shahir Deo wrote:Do we have a Constructors in java ?

Yes, did you mean to ask constructors in servlet?
Shahir Deo wrote:if Yes , Why do we use init to Initialize the Servlet.??

From a programmer's perspective, you only provide the servlet with the information you want in the init() and leave the rest to the servlet container.
If you were to use the constructor, you will have more code to provide information the servlet wants, wasting your time and lines of code.
See the difference?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic