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 392485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:09:49+00:00 2026-05-12T16:09:49+00:00

I currently have a web-app where I have Servlets reading and writing to the

  • 0

I currently have a web-app where I have Servlets reading and writing to the ServletContext attributes and I have “working” Threads (Daemon threads) which get initialized on startup and currently hold as a member the ServletContext object.
For a couple of reasons I’m thinking of moving to “implement Runnable” instead and I’m kind of stuck with the fact that I need a common resource that servlets and non-servlets can use to talk with each other and I feel I’m a bit stuck in the whole ServletContext paradigm.
I’d appreciate it if someone could provide points on why to use the ServletContext (my way or another way) and also I’m thinking to just use a singleton class which will be initialized on startup and will be thread safe and I’ll just route all communication of the servlets and non-servlets through it.
What do you think?

Thanks

  • 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-05-12T16:09:49+00:00Added an answer on May 12, 2026 at 4:09 pm

    Java Servlet container is effectively a lightly managed code container. Servlets are components with very well defined life-cycle, and, there is also a very well defined contract between the container and the components (which can also include filters).

    As it is specified, a web-app is a passive-reactive system.

    It is passive, in the sense that no user defined threads are (strictly) allowed. (Some containers may not enforce this restriction, but by spawning off your own threads, you are effectively off the reservation, and potentially unpredictable behaviors can occur.)

    It is reactive, in the sense that activity on the server (in your code base) occurs in response to requests, and these are for the typical Servlet app HTTP requests.

    The ServletContext is the shared context of all components of a given web-app in the container. This context is created when the web-app is activated, and destroyed when it is deactivated. You can use a ServletContextListener component to hook into this life-cycle and get notification call backs regarding life cycle events.

    If you are willing to continue on the wild side and spawn off active elements in your web-app, then you may wish to consider the following:

    1 – Create and register a ServletContextListner component to manage the active components. On web-app startup/activation you will get a callback from the container. Here you can start your threaded components. As this component will be passed a reference to the ServletContext, you can pass that reference to the threaded components.

    2 – Your threaded component class (whether an extension of Thread or implementation of Runnable) needs to be manageable, in the sense that you need to provide the means to cleanly shutdown the thread when your ServletContextListener receives the context shutdown callback. If you are up to formalizing the runnable tasks, the ExecutorService provides the functionality you require so you don’t have to reinvent the wheel.

    That said, you may wish to review your requirements and thus your chosen design and web-container platform. It may be that you really require a more sophisticated back-end stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a primary Java Web App Project which houses some Servlets, JSPs
I currently have an app deployed on Heroku which runs on two web dynos
I have a Java web app which consists of a variety of a servlets
I have a web app that currently is inserting x (between 1 + 40)
I want to build a REST web service on app engine. Currently i have
I currently have a single web server hosting multiple WordPress sites which use W3TC.
We currently have a .NET 2.0 web app that uses forms authentication via cookies.
I am currently working on a web app that had previously run and deployed
I currently have a web app that is built using Play framework (1.2.4) with
The web-app I'm currently building follows MVC. I've structured the servlets in a way

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.