开发者

How do I keep track of the session for each servlet request, until I use it? Singletons wont work?

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 singleto开发者_如何学Cn (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?


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.


I think what you want is a java.lang.ThreadLocal variable.


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

The Context object enables you to encapsulate state and pass it across tiers, without having to worry 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 to get more information in this area.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜