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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:31:40+00:00 2026-06-01T18:31:40+00:00

The servlet spec (see my previous question) guarantees that the same thread will execute

  • 0

The servlet spec (see my previous question) guarantees that the same thread will execute all Filters and the associated Servlet. Given this, I do not see any use for passing data using HttpServletRequest.setAttribute if there is the option to use a ThreadLocal (assuming you clean up properly). I feel that there are two benefits to using ThreadLocal: type-safety and better performance because no string keys or maps are being used (except probably into a thread collection by (non-string) thread id).

Could someone please confirm if I am right so I can proceed with abandoning setAttribute?

  • 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-01T18:31:42+00:00Added an answer on June 1, 2026 at 6:31 pm

    Is ThreadLocal preferable to HttpServletRequest.setAttribute(“key”, “value”)?

    Depends on the concrete functional requirement.

    JSF, for example, stores the FacesContext in a ThreadLocal. This enables you to access all of the JSF artifacts, including the “raw” HttpServletRequest and HttpServletResponse anywhere in the code which is executed by the FacesServlet, such as managed beans. Most other Java based MVC frameworks follow the same example.

    As per your comment,

    I primarily need to transport the User and EntityManager objects from the user and database Filters to the Servlet. I also find that these are frequently and unexpectedly needed in code further down the line and I am tempted to use them well beyond the Servlet (i. e. in nested code called by doGet). I feel there may be a better way for deeper code – suggestions?

    As to the User example for which I assume that this is a session attribute, I’d rather follow the same approach as JSF. Create a ThreadLocal<Context> where the Context is your custom wrapper class holding references to the current HttpServletRequest and maybe also HttpServletResponse so that you can access them anywhere down in your code. If necessary provide convenience methods to get among others the User directly from the Context class.

    As to the EntityManager example, you could follow the same approach, but I’d personally not put it in the same ThreadLocal<Context>, but rather a different one. Or, better, just obtain it from JNDI in the service layer, which would allow you more finer grained control over transactions. In any case, please make absolutely sure that you handle the commit/close properly. Taking over the persistence and transaction management from the container should be done with extreme care. I’d really reconsider the aversion against using the existing and well-designed APIs/frameworks like EJB/JPA, otherwise you’ll risk a complete waste of time of reinventing all the already-standardized APIs and stuffs.

    See also:

    • Retrieving Web Session from a POJO Outside the Web Container
    • Design Patterns web based applications
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anything in the Servlet spec, Tomcat, or Wicket that will allow a
I know that Tomcat and the Servlet spec do not support starting webapps in
Java EE 6 looks exciting. It will include the Servlet 3.0 spec, EJB3.1 Lite,
When you use JSF, you'll have the controller servlet javax.faces.webapp.FacesServlet that will be mapped
What Servlet Spec and JSP version does Jboss 4.2.3 support?
In the servlet 2.4 spec you can define dispatchers in the filter config [REQUEST
In servlet 3.0 one can use startAsync to put long work in another thread,
I have this java servlet that grabs information from a form, I need to
I have installed a servlet (solr) that requires that I set the variable solr.solr.home
I've written a Servlet that uses the library ganymed-ssh2-build210.jar (it uses these classes: import

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.