permaculture playing cards*
The moose likes Servlets and the fly likes Does servlet need constructor( Have constructor)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Does servlet need constructor( Have constructor)?" Watch "Does servlet need constructor( Have constructor)?" New topic
Author

Does servlet need constructor( Have constructor)?

Laxmikant Kumbhare
Ranch Hand

Joined: Sep 19, 2011
Posts: 38
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Oct 11, 2011
Posts: 98
Hi,
Yes Tim is right But you can create a default constructor ( with no parameter) if you need.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kumar Devesh
Greenhorn

Joined: Nov 18, 2011
Posts: 7

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

Joined: Jan 10, 2002
Posts: 61434
    
  67

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

Joined: Sep 19, 2011
Posts: 38
Thanks to all!
 
 
subject: Does servlet need constructor( Have constructor)?