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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:53:22+00:00 2026-05-26T08:53:22+00:00

I created a class that implemented ServletContextAttributeListener without the default constructor and the container

  • 0

I created a class that implemented ServletContextAttributeListener without the default constructor and the container complained:

SEVERE: Error configuring application listener of …..
java.lang.InstantiationException: …

So I created a default constructor in addition to parameterized constructor and everything worked fine. Since the container requires a default constructs, I suspected that the container is creating a new instance of the class and not using the instance that is already created. But inserting a static object counter member data, I found this suspicion to be true.

My question is: If I want to update the member data of my object on ServletContextAttributeEvent, can I do it in a thread-safe manner? What are the side effects of container creating a new object and what are the workarounds?

  • 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-26T08:53:23+00:00Added an answer on May 26, 2026 at 8:53 am

    Yes, the container instanciates it for you. And yes, this object being unique for the whole webapp (in a JVM), you must make sure everything is done in a thread-safe way, using synchronization. I don’t see any side-effect or workaround. That’s how it’s supposed to be.

    You seem to confuse attributes and listeners. You may set and remove any attribute, of any type, to the servlet context. These attributes should not implement ServletContextAttributeListener.

    You may also, on the other hand, register listeners, either via the web.xml, or by calling one of the addListener methods of ServletContext. Those listeners are not servlet context attributes.

    If you have registered one ServletContextAttributeListener to the servlet context, then each time you set or remove any attribute to/from the servlet context, the listener’s appropriate method will be called. The name and value of the attribute that has been set or removed will be inside the event received by the listener.

    So, since the container handles several requests in parallel, and since each request may set or remove attributes from the servlet context, the listener will be called multiple times in parallel, and must thus be thread-safe.

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

Sidebar

Related Questions

I've created class that takes Exception type in constructor private readonly Exception _exception; public
I created a simple class that shows what I am trying to do without
I've created a singleton class that loads a plist. I keep getting this error
I have created a BasicMembershipProvider class that inherits from MembershipProvider, when implemented, the ValidateUser
I created a class that's something like this: public class MovieTheaterList { public DateTime
I created a class that handles serial port asynchronously. I use it to communicate
I created a class that extends ProfileBase: public class CustomerProfile : ProfileBase { public
I've created a Java class that connects to an IIS website requiring NTLM authentication.
I've created a class library that contains three entity models for three databases that
I have created a class that I've been using as the storage for all

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.