my dog learned polymorphism*
The moose likes Servlets and the fly likes why we need to use servlet context listener ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "why we need to use servlet context listener ?" Watch "why we need to use servlet context listener ?" New topic
Author

why we need to use servlet context listener ?

munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

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 ???



Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
..., or, to put it another way: what you read was incorrect.
munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

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

Joined: Sep 18, 2010
Posts: 69

we can put all the attributes in all the scopes with using the wequest object

then why to use the listeners.....???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

"wequest object"? Are you channelling Elmer Fudd?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why we need to use servlet context listener ?