wood burning stoves 2.0*
The moose likes JSP and the fly likes Is javabean thread-safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Is javabean thread-safe" Watch "Is javabean thread-safe" New topic
Author

Is javabean thread-safe

tuntony wei
Greenhorn

Joined: Jun 29, 2011
Posts: 13
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

Do you have any Java code in your JSPs? Do you have instance variables in your servlets?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

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.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
tuntony wei
Greenhorn

Joined: Jun 29, 2011
Posts: 13
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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?
 
wood burning stoves
 
subject: Is javabean thread-safe
 
Similar Threads
Chapter 6(Session Management) notes (HFSJ) for revision
IBM HttpServer and websphere Appserver
controller creating javabeans
Combining static and dynamic content
generating dynamix menus, headers, and content using static html and jsp