File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is javabean thread-safe

 
tuntony wei
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everybody:
I am new to jsp/servlet and feel confused about the javabean used among JSPs and Servlets. I have a web app, I do following things:

1. StatusBean statusBean = new StatusBean(); // this is a local variable
2. statusBean.setXXX
...
3. statusBean.setXX
...
4. session.setAttribute("statusBean", statusBean);

And then, I will pass this session to few JSPs and servlets, and some fields in the statusBean might be changed in the following jsp/servlet, and finally, I finished some works and get result from this statusBean.

To me, when different clients visit the servlet, they got different sessions. So because of different sessions to different clients, the web container guarantees the statusBean is always different to different client. Am I right on this? If this is ture, I do not need worry too much. However, when I test my app, in some situation, client A's session shows client B's statusBean data. (I found this problem but it is not reproduced on every test) So, what should I do to gurantee client A always doing his job with his own statusBean, and client B doing with his too?

Thank you very much.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any Java code in your JSPs? Do you have instance variables in your servlets?
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JavaBean is not thread safe. It is good to initialize it as a local variable and set it from there. The instance state of your statusBean will not be visible to other clients.

I would advise you to copy and paste your jsp, servlet and web.xml here in code tags, so we can tell you what the problem is.
 
tuntony wei
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys
Thank you very much. I do not have any instance variables in my servlet and these 2 days, I tried to reproduce the problem so that I can narrow down the problem and post relevant code here. But it is really difficult to reproduce it and seems it only happens when there are many users. As my boss suggested, if not occurs freqently, we can ignore. Not a good boss, right?
 
Paul Clapham
Sheriff
Pie
Posts: 20171
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's still a symptom of data being shared across threads. Perhaps you have static variables somewhere, like in one of your beans? Perhaps you're putting data in the application context?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic