aspose file tools*
The moose likes Servlets and the fly likes Session problem in Enterprise Java / Weblogic application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session problem in Enterprise Java / Weblogic application" Watch "Session problem in Enterprise Java / Weblogic application" New topic
Author

Session problem in Enterprise Java / Weblogic application

Jo
Greenhorn

Joined: Jun 16, 2002
Posts: 2
Background: we are using session variables in our enterprise java application running Weblogic application server.
Issue: When we have more than one browser open in the same PC there is session variable conflict.
Example:
1) Variable v1 of browser b1 has state X.
2) We open browser b2 and run the application, the variable v1 has session Y.
3) We come back to browser b1. We notice that v1 has state Y, we are whereas expecting it to be X.
Question:
How do we maintain the various instances of session variables when using multiple browsers from a single PC?

N.B. But this is not an issue when we check the application from multiple PCs each with a single browser open.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Welcome to the JavaRanch! Please adjust your displayed name to meet
the JavaRanch Naming Policy. You can change it here.
Thanks!
and welcome to the JavaRanch!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Why do you require multiple sessions at the same time on the same PC?
In theory, on way is to open (for example) IE, then create another instance by launching the application again (ie clicking the icon as opposed to CTRL-N)
Never had to do it (and not really what sessions were designed for) so no idea and if that doesn't work, you may be in trouble...
I suppose another way would be to explicity manage state through a user's visit: give them a unique number and make sure they (explicitly) carry this value around - you can't write it to the session! Then each time a request came you could see which part of the session it came from. In this case they would share the same session but you would be able to tell them apart.
Dave
Erick Jones
Ranch Hand

Joined: Jun 17, 2002
Posts: 38
I have encountered this same problem. The way I solved it is as David suggested. Except it is not quite that simple. The number that you generate needs to be unique for each browser instance. This number is kept with the browser via a hidden field in the request. With each form submission, the hidden field is taken out of the form and placed into the request. I also created a helper class to get/set attributes into the session. The helper methods append the unique number to the key for each get/set. In a sense, this partitions the data in the session by application instance.
This only solves the problem if a user launches the application twice. If they simply open a new window using ctrl-n, then there is no way to know that you need to generate a new number. I have chosen to live with that restriction, and I would bet that many other applications have the same problem.
If you launch two separate browsers, I think IE will create two separate sessions. Whereas, netscape will share the same one. It could be the other way around, I don't remember. If you just open another window, then I believe both windows(in both IE and Netscape) will share the same session.
Hope this helps.
Regards,
Erick
Jo
Greenhorn

Joined: Jun 16, 2002
Posts: 2
Hi Erick,
Could you send me the code snippets?
Thanx in advance,
Erick Jones
Ranch Hand

Joined: Jun 17, 2002
Posts: 38
The code is pretty integrated into our system, but let me try to help you out a little more.
First off, in order to generate the unique id, you need to know when an application is being launched. If the only GET request coming into your servlet is when the application is launched then this is pretty simple. This id only needs to be unique for a single user. The id calculation I use is the number of seconds that have transpired for the current day. This assumes that the session will expire in less then 24 hours, and that a single user cannot launch the application twice in the same second (both are pretty safe assumptions). The calcuation is as follows:
long id = System.currentTimeMillis() % (1000 * 60 * 24)
Since you need this value to get/set values from/into the session, you cannot store this id in the session. You can place it into the request. Each JSP needs to take the value out and place it into the form as a hidden field. Each post to your servlets needs to take the value out of the form and place it back into the request.
On application launch, in your servlet:
request.setAttribute("id", id);
In every JSP:
<input type=hidden name="id" value="<%=request.getAttribute("id")%>">
In all posts back to your servlets:
String id = request.getParameter("id");
request.setAttribute("id", id);
Now, create a helper to access the session:
public abstract class HttpSessionHelper
{
public static void setAttribute(HttpServletRequest req, String key, Object obj)
{
String id = request.getAttribute("id");
HttpSession sess = req.getSession();
String newKey = id + key;
sess.setAttribute(newKey, obj);
}
public static Object getAttribute(HttpServletRequest req, String key)
{
String id = request.getAttribute("id");
HttpSession sess = req.getSession();
String newKey = id + key;
return sess.getAttribute(newKey);
}
}
This helper should always be used to access the session so that collisions are avoided.
Hope this gets you started!
Regards,
Erick
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session problem in Enterprise Java / Weblogic application