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

can there be sevlet constructor with parameters

 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can there be sevlet constructor with parameters?
 
Ankit Garg
Sheriff
Posts: 9520
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic