aspose file tools*
The moose likes Servlets and the fly likes constructor in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "constructor in servlet" Watch "constructor in servlet" New topic
Author

constructor in servlet

Harjot Narula
Greenhorn

Joined: Sep 22, 2003
Posts: 11
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

Joined: Jan 14, 2003
Posts: 171
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


Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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.


Groovy
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
rajanidhi rajasekeran
Greenhorn

Joined: Aug 28, 2003
Posts: 4
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

Joined: Aug 18, 2003
Posts: 218
OR override init() method of GenericServlet in which you don't have to call super.init(config)
Thanks
-------------
Sainudheen
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: constructor in servlet