This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Does servlet need constructor( Have constructor)?  RSS feed

 
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?
 
Saloon Keeper
Posts: 4745
117
  • 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.
 
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.
 
Author and ninkuma
Marshal
Posts: 66804
168
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.
 
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
Posts: 66804
168
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!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!