Search code examples
javajakarta-eestatic-variables

Sharing variables between classes in Java EE


I'm developping a Java Web Application and I have some problems with sharing variables. To the best of my knowledge, there is two ways : (1) by using static variables, then each class can access to the given var, (2) by using setter and getter.

I have an issue for each method. With the first one, all users (even with different session ids) will share the same values. And, for the second one, access via getters force the program to use constructors and in this case we will loose the values we previously got for current user.

So, anyone can explain me how to share information only for a given sessionId?

To be more explicit :

I tried to seperate the model from the view aspect of my application. So, the package Model will contain all databases access or Web Service requests for example, and the View package will contain all servlet pages and user interactions with the programme. When the user select an option, this information is stored in a public static var then the other classes can access. But conflicts occur when two users select two different options. Cause the variable is static... I need a variable which can be shared between classes and keep its value only for a given user


Solution

  • Your first option listed, using static variables to store mutable state, doesn't work in a Java EE web application. You're right that different users on the same server will see the same values. In addition to that, if multiple instances of your application are deployed, the static variables on one server can contain different values from what they contain on another server, so if you have several boxes, each with an instance of your web application, and a load balancer in front of them sending requests round-robin, users can see different state for different requests. So you can exclude that option.

    Your second option, "(2) by using setter and getter", isn't clear at all.

    If you want to keep state where it is visible to only one user, you can put it in the HttpSession. Things stored in the HttpSession are specific to the logged-in current user. This is a temporary storage spot only, when the session times out the contents of the session go away. Putting too many things in the HttpSession can be a problem; it can take up a lot of memory, and can cause slowdowns in a clustered application as the nodes try to copy information between nodes to keep every server's session contents current. (An alternative is pinning users to a given server with the result that load is distributed unevenly.)

    It can be preferable to keep user-specific information in the database. That way the data is in one place that is available to all instances of the application, you can cache it like any other data you get from the database, and your application can make sure only the appropriate user sees it. Also the data is persistent instead of going away when the session times out.