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

Why init() if constructor is there

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic