• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why we need to use servlet context listener ?

 
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read the that , we have to use ContextListener for getting the database values(of other java class) into the server

but the same thing can be done in the Servlet constructor .

then why need to use the servletContextListener ???



 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, you should do stuff in the Servlet init() method, not the Servlet constructor. That gives you access to the ServletContext.

Secondly, doing work there is fine if only one servlet is going to need it. But in many applications you're going to have things - like database details - that are needed by several servlets. But you only want to do it once. Using a ContextListener allows you to perform actions after the context is ready, but before any servlets are created.
 
Saloon Keeper
Posts: 7585
176
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..., or, to put it another way: what you read was incorrect.
 
munjal upadhyay
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Firstly, you should do stuff in the Servlet init() method, not the Servlet constructor. That gives you access to the ServletContext.

Secondly, doing work there is fine if only one servlet is going to need it. But in many applications you're going to have things - like database details - that are needed by several servlets. But you only want to do it once. Using a ContextListener allows you to perform actions after the context is ready, but before any servlets are created.



what if I want to use diffeerent databases for different servlets.......

 
munjal upadhyay
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we can put all the attributes in all the scopes with using the wequest object

then why to use the listeners.....???
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There servlet context listener is designed to inform your application when the servlet context is put into service, and when it is taken out. It is the correct way to perform context-wide setup and shutdown activities.

Sure, you could rely upon side-effects like a servlet being put into service, but that's not only sloppy but could cause problems as a container is free to take a servlet out of service and then put it back anytime it likes for whatever reason.

The bottom line is that you should always use the correct tool for a job and not rely upon hacks that seem to work but can cause unexpected and hard-to-diagnose problems.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"wequest object"? Are you channelling Elmer Fudd?
 
reply
    Bookmark Topic Watch Topic
  • New Topic