• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we need to use servlet context listener ?

 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • 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 ???



 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Tim Moores
Bartender
Posts: 2946
46
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..., or, to put it another way: what you read was incorrect.
 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • 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
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • 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.....???
 
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
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
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
"wequest object"? Are you channelling Elmer Fudd?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic