Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which method call first?

 
Arun shan Kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
can we have use constructor in the servlet with no arguments. if i
use that which method will call first init() or constructor.

thanks in advance
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can provide a no-argument constructor if you wish. The constructor is always invoked first, when the servlet is instantiated (just like any other object). The init(ServletConfig) is then invoked by the container, followed by init() if you're using a GenericServlet (this class's implementation of init(ServletConfig) delegates to init()).

If you are using a constructor however, you need to be careful about thread safety issues - try to avoid using instance variables - and since the ServletConfig, and therefore ServletContext also, are not available in the constructor, it usually isn't of much use.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic