Stefan
Stefan

Reputation: 14863

Server session managment in Java

How can I handle static varibles inside my server session?

I have 30 different functions with no real realtion on the server, but they currently share data over some static varibles (I know this is bad, but I was just testing it). Now if I have different clients connecting to the server, they would interfear with each other an no client would get the right data.

So I wondered what a good way to handle this diferent Session datas without passing twenty refernce to the other objects to a class when creating a class.

Another problem is, that the classes instanciate threads, so I can't be shure, that the actions are completed when I send the responses back to the server. (So switchin the current static varibles depending on the client is not an option)

Upvotes: 0

Views: 815

Answers (1)

Bozho
Bozho

Reputation: 597096

If you don't want to put all these things in the session (session.setAttribute(..) and .getAttribute()) then perhaps you can use a map of maps:

public static Map<String, Map<String, Object>> sessionValues = ...

and set/get from that map. Where:

  • the key of the 1st map is the session id (can be obtained via session.getId()
  • the key of the 2nd map is the property name
  • the value of the 2nd map is the property value

Thus you will be able to have values unique to sessions without relying on the servlet API.

Upvotes: 2

Related Questions