File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » JSP
Bookmark "Is javabean thread-safe" Watch "Is javabean thread-safe" New topic

Is javabean thread-safe

tuntony wei

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

Joined: Jan 10, 2002
Posts: 63858

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

[Asking smart questions] [About Bear] [Books by Bear]
Dieter Quickfend

Joined: Aug 06, 2010
Posts: 543

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

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

Joined: Oct 14, 2005
Posts: 19973

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?
I agree. Here's the link:
subject: Is javabean thread-safe
It's not a secret anymore!