• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init method or constructor?

 
Kalai Selvan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why init method is required in servlet, can't constructor serve the same purpose?

Thanks in advance.

Kalai Selvan T.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because servlets are created according to textual data in web.xml or a URL, so instances are created like this:



which uses the no-argument constructor. But a servlet needs various initialization parameters to set itself up, so the designers of the servlet APIs added an init() method to which the container could pass the initialization arguments.

Now, in JDK1.2, the java.lang.reflect package was added, and it's now possible to do the equivalent of passing constructor arguments to an object created via newInstance(). But the servlet API was first defined before JDK 1.2 came out, so the init() method was the only way to do things.

Note that applets also have an init() method, for very similar reasons.
 
Kalai Selvan
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
Thanks for the brief explanation.....

Kalai Selvan T.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic