• 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

constructor in servlet

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Just out of curiosity, I want to find out why do we use an
init method to create an instance of servlet instead of using a
constructor.
Why cant we use constructors in servlets.
Harjot
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instances of Servlets are automatically created by your Web Application Server e.g. Tomcat and this is done using a 'ServletFactory' that automatically invokes the constructor on the servlet. Once an instance of the servlet is created the application server will invoke the init() method.
The init() method does not create an instance it is merely a standard method that you can guarantee will be invoked AFTER an instance of the servlet has been instantiated.
HTH
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets, like Applets, are hosted by a container that has to create them from a textual description -- i.e., the container has the name of the class in a web.xml file, and has to create an instance of that class. It will of course use reflection to do this. It's much simpler to say

than it would be to create the class, call "findDeclaredConstructors" to get one specific constructor, then call it, and then cast the result to a Servlet. It could be done, it's just simpler this way.
Back when Applets were designed, there was no reflection API, and so the technique above was the only way to do it.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Back when Applets were designed, there was no reflection API, and so the technique above was the only way to do it.


What about Servlets? When they were out was the reflection API available.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason init() is used instead of constructors is that servlets implement the javax.servlet.Servlet interface and interfaces can't except parameters in their constructors
Correct me if I am wrong, since we can we implement the interface rather than extend HttpServlet?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say yes, you're right. There could be a convention that Servlets had a constructor that accepted a ServletConfig, and the container could report an error if it failed to find such a constructor -- but the init(ServletConfig) technique has compile-time safety and the container code is much simpler.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 9.1.3 of the Java Language Specification indicates that an interface can not have constructors, which is quite intuitive because an interface is not meant to describe the implementation (and thus, creation) of functionality.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet is Dynamically generated,we cannot have a parameterized constructor for a dynamically generated servlet.We will get the servletconfig object only in the init method .so in order to get this object we will give like this
public init(servletconfig sc)
{
super.init(sc);
}
we can have a parameterized init method.
that is why we are using init instead of constructor.

If i am wrong plz correct.
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OR override init() method of GenericServlet in which you don't have to call super.init(config)
Thanks
-------------
Sainudheen
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic