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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:02:46+00:00 2026-05-15T01:02:46+00:00

For each servlet request I get, I pass, perhaps, 10 methods before I am

  • 0

For each servlet request I get, I pass, perhaps, 10 methods before I am at where I need to check something in the session and I need the HttpSession.

The only way I can get the HttpSession is from the HttpServletRequest, correct?

How do I keep track of the session for each servlet request? Unfortuantly I cannot simple make a singleton (ex, SessionInformation.instance().getAttribute(“name”)) because that session would then be used over all requests.

Is there a way to store the session globally for each request without having to pass it (or it’s information) down all the methods just in case I need it?

Update 1:

Alohci pushed me into the right direction. I need something that can either:

1) Manage to store variables globally only over a Thread. A “Singleton-per-Thread” solution?

2) Or is there a unique id for each thread/request a servlet gets? If so, what?

  • 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-15T01:02:46+00:00Added an answer on May 15, 2026 at 1:02 am

    How do I keep track of the session for
    each servlet request?

    The server identifies its users by marking them through a cookie that holds a session ID on their browsers. On the server the session is where you place data that you need to keep track of during the client/server interaction (to give state to the application).

    session.setAttribute("name", SerializableObject)
    

    Note: Because App Engine stores session data in the datastore and memcache, all values stored in the session must implement the java.io.Serializable interface.

    You can then grab the session out of a HttpServletRequest by doing

    request.getSession()
    

    see javadoc: here

    After you retrieve the session inside your servlet, you use session.getAttribute("name") to retrieve whatever data you were interested in to keep track of the interaction.

    Is there a way to store the session globally for each request

    Why store it globally when the only thing that has the ID to access the session is the servlet that got a request that came with that session ID. Not everything in your application needs to know about every request.

    Here is an example of a request that comes with a session ID cookie:

    GET /something.htm HTTP/1.1     
    Accept: www/source     
    Accept: text/html     
    Cookie: sessid=123456789
    

    You can’t store this information globally, it is unique to each request.

    Without having to pass it

    When doing useful things with the data, don’t pass the session to your methods, pass the useful information, like user name, shopping cart content etc.

    HttpSession session = request.getSession() //inside a servlet
    String userName = session.getAttribute("name");
    List<ShoppingItems> items= session.getAttribute("cart");
    
    MakeTheUserPay(userName, items) //no session passing here
    

    Your business logic methods shouldn’t even know what a session is. It’s more like an artefact.

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

Sidebar

Ask A Question

Stats

  • Questions 442k
  • Answers 442k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you've asked for is not possible in base PHP.… May 15, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer You can download it from this page. What crash issues… May 15, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer EDIT There's a simple way to do it. In your… May 15, 2026 at 5:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.