I'm having an issue with JavaBean synchronization working with my serlvets and JSPs. The structure of my application is HTMLPAGE->SERVLET1(PROCESS DATA)->JSP->SERVLET2(process data)-> JSP->SERVLET3->SAVE TO DATABASE->Send EMAIL via JSP page mailer. To get through the pages, I use a java bean to maintain the state of the data collected. My java beans have all public methods marked as synchronized, and I have all the fields in my bean class as private. I have a default no arguement constructor. This bean is used by my servlets to store data temporarily. This works great when there is one person entering data at a time. The issue is that if two people log in concurrently to the same set of servlets, and start entering data, the person that started first- their data will persist for both their own session, as well as the other person's session.
What you are describing is a bean with session scope - you should be storing a unique instance in an HttpSession. If the bean is only used by one Thread there is no need for synchronization. It sounds like you are keeping a reference to the bean as an instance variable. Bill
In terms of storing for the session- you are correct- In each of my servlets, I store the session via: HttpSession session = request.getSession(true); session.setAttribute("test", test); In the JSP's I get the session by giving the beans a scope of the session, and checking whether the session is new or not(if it is new, then I invalidate the session(with the exception of the first servlet) and don't allow further processing). In terms of instance variables, I'm declaring each of the fields as private and static- so they should be class specific. So your saying that I should not have them as static, to enable each instance to have a copy of them? Is this correct?
Matthew X. Brown
posted 19 years ago
No need to reply, I just tested the scenario with the changes to the fields so that they are not static and it now works fine. Thanks for helping me realize it was something very basic- I was thinking sessions and stuff, and just overlooked it.