Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why init() if constructor is there

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why init is required if allmost all things i can do in my constructor of servlet.(i think only ServletConfig is not available in constructor).
 
Vince HW Chan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is interesting, my guess is it may be,

Servlet, just like EJB, is supposed to be instantiated by container.
And, they are not supposed to " throw exception" while they are instantiated. If you have operation which may throw exception inside the constructor, it fails. Or, if you catch the exception within the consturctor, can you sure the servlet still "valid"?

However, init() declare to throw ServletException, which allow you to stop servce any requests if the setup operations throw excpetion or error occurs.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj Kumar Bindal:
i think only ServletConfig is not available in constructor


You answered your own question.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You answered your own question.

But that doesn't really answer it: why is there a separate init(ServletConfig) method? Why not just have a constructor with the ServletConfig parameter?

My guess is that it gives a clean set of lifecycle methods: the constructor is used for instantiation, the init method for initialisation. Perhaps also they thought they might add alternative init methods (with different parameters), and having extra constructors around would then just be cumbersome? I think the point raised about throwing exceptions is a good one.

But like many things in life, that was just the decision that was made and we learn to live with it... there's no point arguing, as now it's established it's never going to change!
 
Manesh Kumar
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to look at this, though this might not be answer.

When servlets were first introduced, there were more Applet programmers. Just for maintaining coding style of Applet with servlet and easier switch over for Applet programmers(where they used init method for initialization), this could have been introduced for convenience to the java programmers.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructing a servlet instance is (or can be, I think it's up to the container to decide what to do) a 2 stage process.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other issue here is that specifying that a subclass must have a constructor with a particular signature is a design that can't be enforced. That's just one more thing to go wrong. Whereas an "init" method can be specified in the superclass, or the interface, and if subclasses need to override it they can. But if they don't override it, there is no problem either.
 
Neeraj Mariyana
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is because the Servlet is instantiated by the container using the Class.forName(<servlet class> .getInstance().
For it to do this the servlet should have a no argument constructor. Hence ServletConfig cannot be passed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic