• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Session Problem  RSS feed

 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my application, Currently if there are two users logged in....

And after the second user home page loads. There is a problem.

Now when you refresh the page of First user, The data for second user loads up...

I've stored a HashMap in a session attribute once it gets filled with data.

So after second user logs in and if refresh the First users home page,
Second user data is coming in First....

I also tried it on Different browsers like Firefox and IE7, still problem persists...

Please help..!

Thanks,

Aditya Kanitkar


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chances are you have a thread safety issue.

Do you have Java code in your JSPs? If so, you shouldn't, and the use of declaration scriptlets can cause such problems.

Do you have instance variables in your servlets? If so, they are a problem.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your immediate reply....

Ya, I've java code in my JSP, But it only calls a method which gets data into a HashMap, Nothing else...

And that HashMap is declared in JSP but not like this....



And its like this


So for avoiding repeated calling of that method myObj.getMyMap();
I have stored it in a session attribute

So it goes like this...




I've done that like this.

 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Kanitkar wrote:


What is myObj on line 04? and should line 09 be "getAttribute("myMap");" instead of set?
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 09
That was a writing mistake... Its edited now....

And on line 04
myObj is a object of a class where code written to bring the data into hashmap

I dont know why its happening like that....

 
Smitesh Shinde
Ranch Hand
Posts: 45
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya,
You are using same key for string attributes for all the user. So your session data is getting overwritten. Remember you should use unique key per user.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Smitesh Shinde wrote:Aditya,
You are using same key for string attributes for all the user. So your session data is getting overwritten. Remember you should use unique key per user.


Do you mean something like this...???




@Smitesh: Are you suggesting like that...??




 
Smitesh Shinde
Ranch Hand
Posts: 45
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

And Can't you use username as a key?
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol, that's funny. You didn't have any problem with your original code in term of accessing the session scope. Two things:
1. How did you create the myObj object? Is it a new instance everytime a user logs in? Or is it created when the application starts?
2. How did you test the two sessions scenario? From 2 browser windows on the same machine or from two different machines?
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Smitesh: you know that session is per user, right?
 
Smitesh Shinde
Ranch Hand
Posts: 45
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I know it. Probably I was thinking of something else rather than concentrating on the fact that it's session attribute.

Yes you are right it has something to do with the way myobje is getting populated.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Vo wrote:Lol, that's funny. You didn't have any problem with your original code in term of accessing the session scope. Two things:
1. How did you create the myObj object? Is it a new instance everytime a user logs in? Or is it created when the application starts?
2. How did you test the two sessions scenario? From 2 browser windows on the same machine or from two different machines?



There was one issue...

Now i've declared myObj like this




But, still problem persists and scenario is like this

Two user logs in separate machines

And still same problem is happening.

Currently, i have not modified the attribute names....




 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, do you use static variables anywhere in the class MyClass?
How did you pass parameters to myObj? particularly the getMyMap() method? Your sample code doesn't show how that method provides you different Map objects for different users.
Is this a real code or just an abstract code?
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Vo wrote:Alright, do you use static variables anywhere in the class MyClass?
How did you pass parameters to myObj? particularly the getMyMap() method? Your sample code doesn't show how that method provides you different Map objects for different users.
Is this a real code or just an abstract code?


And i had some static var i.e. some collections like arraylist and maps...

Now i've removed it....

Still its not working....

And yes, its a real code... The data we bring over here is dependent on login name

I get that login name in myClass...

But, I think that is not related to this problem....
It gets appropriate user....
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, be patient, the problem seems to be with MyClass code. Let me rephrase my question: How do you pass the username into to myObj instance?

There are two possible problems there:
(1) The method myObj.getMyMap() return the same object reference every time. Hence when you change data to the next user, all the previous users will only see the that user data.
-> it'll be easier, if you post the code of the getMyMap() method here.

(2) The session is cleaned in the beginning of the page somehow hence myObj.getMyMap() got triggered every time and because MyClass isn't implemented properly it returns the last user data.
-> You should put some indicator on line 04 of the JSP so that you can check if it is executed every time
-> it'll also be easier, if you post the code of the getMyMap() method here.

If MyClass isn't too long, post the code for the class here as well.
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank god... Problem has been resolved now...

Thank you Guys....

It was related to, the way objects were declared.....

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!