Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7746535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:18:13+00:00 2026-06-01T10:18:13+00:00

I need to release some resources in my web application (Tomcat 7). I try

  • 0

I need to release some resources in my web application (Tomcat 7).
I try to do it in the destroy of my servlet.
The problem is that when I do getServletContext() in my destroy nothing happens.
By debugging I managed to see:

java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:125)

Originating from my line getSerlvetContext() in the destroy method of my servlet.

So right now, I have no idea how am I supposed to do clean up in my web application.
I have stored some resources in the ServletContext so that it can be used from anywhere in my application and as far as I know
the resource clean-up should be done either

1) in the destroy of servlets or
2) the contextDestroyed of an ServletContextListener

But neither of these seem to work properly. In case (1) I get the NullPointerException when trying to access servlet context.
In case (2) the web application is already shutdown so if I have static methods responsible to do e.g. reallocation of DB connections etc the classes (as I have understood the problem) have already been unloaded by the JVM since the web app has been already shutdown.

Am I doing something wrong? What should I be doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:18:15+00:00Added an answer on June 1, 2026 at 10:18 am

    To get the ServletContext, the usual way is to store it in an instance variable. Depending on whether you extend Servlet or implement a ServletContextListener, I would recommend to create an instance variable for the context and then store it from one of these methods:

    • ServletContextListener.contextInitialized(ServletContextEvent sce): Use sce.getServletContext() to obtain a reference of the context, which you can then store in an instance variable.
    • HttpServlet.init(ServletConfig config): Call the super method (init) with the config param, so you can later access it using getServletContext(). If that doesn’t work, you can store the context in an instance variable using config.getServletContext().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some resources that I allocate when my web service is created that
My program need to occupy some resources. I want it to release those resources
In the documentation I have read that I don't need to release the NSOperation
Possible Duplicate: Do I need to disable NSLog before release Application? I wonder if
How can I detect that the client side of a tomcat servlet request has
The destructor should only release unmanaged resources that your object holds on to, and
We have a web application ( Java + Tomcat + Spring + Maven )
Do I need to release a Core Foundation objects to clear up memory? And
Do I need to release a CGMutablePathRef when I'm done with it? If so,
I was wondering if I need to release a copied NSObject? For example, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.