Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

JavaBeans, JSP,and Servlets

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

------------------
author of:
 
Matthew X. Brown
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic