Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor in servlet

 
Harjot Narula
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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.
 
rajanidhi rajasekeran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OR override init() method of GenericServlet in which you don't have to call super.init(config)
Thanks
-------------
Sainudheen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic