Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Servlet or ServletContextListener

Posted on 2006-06-13
4
Medium Priority
?
901 Views
Last Modified: 2013-11-24
I have code that needs to run at application startup and shutdown in my web application. I currently am handling it in servlet. At startup I instantiate some singletons and "initialize" them. At shutdown I have the singletons perform some "clean up" operations.

My question is would it be better to do this with a ServletContextListener? If so, why?
0
Comment
Question by:FeralCTO
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 375 total points
ID: 16900088
I'd use a servlet as your are already doing and store the instance in the ServletContext (if not already there) to make it available to the application. This would ensure there was only a single instance.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16900654
>> would it be better to do this with a ServletContextListener?

You can use it, and implement the contextDestroyed () and contextInitialized () methods to take care of that.

>> store the instance in the ServletContext

Yes, that's fine for storing the singleton but you need to trap the shut-down event too....
0
 
LVL 28

Expert Comment

by:rrz
ID: 16903730
>My question is would it be better to do this with a ServletContextListener? If so, why?  
Yes, but only slightly better. I quote the API
"All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized."  
So I guess it depends on whether you intend to use any of your singletons in any Servlet or Filter init methods.   rrz
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
A solution for Fortify Path Manipulation.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question