File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Contructors in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Contructors in Servlets" Watch "Contructors in Servlets" New topic

Contructors in Servlets

Shahir Deo
Ranch Hand

Joined: Mar 19, 2013
Posts: 80

Do we have a Constructors in java ? if Yes , Why do we use init to Initialize the Servlet.??

Knowledge enlivens the soul.
kartik hedau

Joined: Feb 12, 2013
Posts: 8
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

Joined: Jun 06, 2007
Posts: 2851

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?

|Asking Good Questions|
I agree. Here's the link:
subject: Contructors in Servlets
It's not a secret anymore!