• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does servlet need constructor( Have constructor)?

 
Laxmikant Kumbhare
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me that

Does servlet have controller?
It really needs it?

If not how it loaded in memory?

if yes then what is purpose of init() method?
 
Tim Moores
Bartender
Posts: 2946
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet should never have an explicit constructor. That guarantees that the JVM can instantiate it with the implicit no-arg constructor. The code that would normally go into the constructor in some other class should go in the init method of a servlet class.
 
Miku Ranjan
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes Tim is right But you can create a default constructor ( with no parameter) if you need.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miku Ranjan wrote:Hi,
Yes Tim is right But you can create a default constructor ( with no parameter) if you need.

But the point is that this is rarely needed. In over 10 years of writing servlets, I have never created one with a constructor.
 
kumar Devesh
Greenhorn
Posts: 7
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though Servlet is a class
It is run under an environment ie Web container
Whenever any component run under Environment it has a life cycle.
In case of servlet
1 .first container instantiates the servlet class
using no-arg constructor
(if it fails it gives InstantiationException...you can try this by making your constructor private)

2. Then container call init() method
(init() method can be use to create database connection etc)
3.then Container create HttpServletRequst and HttpServletResponse object and involk the Service() method using them

4.when web container is shut down (ie Tomcat etc) ,container calls destroy() method of servlet


Here step 1,2,4 are called only once in the lifecycle or servlet
where as service() method ie step 3 is called for every new request.
another example of class which runs under environment is
Applet
where envioronment is Java enabled Browser
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
navyblue ramesh wrote: 4.when web container is shut down (ie Tomcat etc) ,container calls destroy() method of servlet

Not quite correct.

The application, and hence the servlet, can be taken out of service without shutting down the container.
 
Laxmikant Kumbhare
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic