aspose file tools *
The moose likes Servlets and the fly likes can there be sevlet constructor with parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "can there be sevlet constructor with parameters" Watch "can there be sevlet constructor with parameters" New topic
Author

can there be sevlet constructor with parameters

anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
can there be sevlet constructor with parameters?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9325
    
  19

There can be, but the container can only call the no-arg constructor, so if there isn't a no-arg constructor in the servlet, the container won't be able to instantiate it for servicing requests...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Well, the Servlet is instantiated by the Container, as long as you have a default constructor you could define any additional constructor that you want, but they will never be invoked, because the container will not use them and you cannot instantiate servlet yourself (at least not if you intend to use it in the container :-) )

You have other alternatives for that. The Servlet interface has a method named init(ServletConfig).

That init method is invoked when the servlet is first created by the container. You can use that method to perform any servlet initialization or read initialization parameters that you have configured in the web.xml file.

Take a moment and read the Servlet specification, the section Servlet Interface, subsection Initialization.

I hope that helps!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can there be sevlet constructor with parameters