1

In my web application, I create new Hashmap and save it in session in first Servlet and use it in the next servlet. In the next servlet i am performing some java script function on the map values and hence updating the map and putting again the updated map in the session . Finally i am displaying the new values in the jsp and submit which will update the new values in the server and thank you page will come.

Note here : It is a user based application, so every user will login and a new hasmap is getting created and storing values specific to the user. Than i am putting that in session which will be used further in application.I am using session.setattribute("map",mapobject) and session.getattribute("map").

So my questions are :

  1. Does this map need to be synchronized ? Please not that i am creating a new object of HashMap when user logged In.

  2. Does the session approach looks good ? I am setting map in session.setAttribute("map",mapobject) ...so if multiple user logged in at the same time will this work for them and store there specific values in the session "map" or it will do some conflict with other user sessions.

Raul Guiu
  • 2,374
  • 22
  • 37
gSingh
  • 57
  • 1
  • 14

2 Answers2

2
  1. The Map might still need to be synchronised if a single user can make multiple requests at the same time: browsers often make up to 4 concurrent requests to a given domain all at the same time. This may (AJAX requests) or may not (images, probably) affect your application meaningfully. But if it does happen, you could get a nasty surprise.

  2. Rather than put a map in the session, since the session basically is a map, you can just put a bunch of entries into it. This might help with the first point, as access to the session is probably synchronised already.

  3. Note that if you ever change this application so that it's hosted on multiple machines, you now have to worry about session replication and/or sticky sessions. This is why I generally advise against using sessions wherever possible (stick the data in a database, instead)... but solutions for that are probably out of scope for this question.

Sean Reilly
  • 21,526
  • 4
  • 48
  • 62
  • you said "Note that if you ever change this application so that it's hosted on multiple machines" ----- Can you explain this hosted on multiple machines ? I am having a jar file created fro my application and that i will gonna put in the clustered servers (hence two machines ) . Also, on login i am putting the map values in session so i think one user can not make multiple requests at the same time. Thanks ! – gSingh Apr 07 '14 at 12:32
  • Modern webapps are served by multiple machines via front end load balancers. – Sean Reilly Apr 07 '14 at 12:40
  • Also, one user can *always* make multiple requests to the same server at the same time if they have access to the session cookie. There's nothing in the architecture of the web that prevents it. – Sean Reilly Apr 07 '14 at 12:41
0

Every request has a unique session with session id so no conflict with session.setAttribute("map",mapobject)

Deepu--Java
  • 3,742
  • 3
  • 19
  • 30