How do I keep track of the session for each servlet request while I'm not using it? Singletons won't work?

For every servlet request I receive, I pass maybe 10 methods before I am where I need to check for something in the session and I need an HttpSession.

The only way to get the HttpSession is from the HttpServletRequest, right?

How can I track the session for each servlet request? Unfortunately, I can't just do a singleton (ex, SessionInformation.instance (). GetAttribute ("name")) because this session will be used in all requests.

Is there a way to store the session globally for every request without having to pass it (or its information) across all methods in case I need it?

Update 1:

Alohchi pushed me in the right direction. I need something that can:

1) Control the storage of variables globally only per stream. "Singleton-per-Thread" solution?

2) Or is there a unique ID for each thread / request that the servlet receives? If so, what?

+2


a source to share


3 answers


How do I track the session for each servlet request?

The server identifies its users by placing them through a cookie that contains the session ID in their browsers. The server in the session hosts data that needs to be monitored during the client / server interaction (to indicate the state of the application).

session.setAttribute("name", SerializableObject)

      

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

Then you can grab the session from the HttpServletRequest by doing

request.getSession()

      

see javadoc: here

After you get the session inside your servlet, you use it session.getAttribute("name")

to get any data you are interested in to track the interaction.

Is there a way to keep the session globally for every request



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

Here is a sample request that comes with a session id cookie:

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

      

You cannot store this information globally, it is unique for each request.

No transmission

When doing useful things with data, don't go through the session to your methods, pass in useful information like username, shopping cart contents, 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 don't even have to know what a session is. It looks more like an artifact.

+6


a source


I think you want the java.lang.ThreadLocal variable .



+2


a source


I think you need to use a Context Object to pass state between your presentation tier and the business tier (I'm assuming it exists).

The Context object allows you to encapsulate state and pass it across layers without worrying about how the state itself is maintained. You can refer to the Context Object Design Pattern in the Core J2EE Patterns book if you need more information in this area.

+1


a source







All Articles