Sharing variables between classes in Java EE -


i'm developping java web application , have problems sharing variables. best of knowledge, there 2 ways : (1) using static variables, each class can access given var, (2) using setter , getter.

i have issue each method. first one, users (even different session ids) share same values. and, second one, access via getters force program use constructors , in case loose values got current user.

so, can explain me how share information given sessionid?

to more explicit :

i tried seperate model view aspect of application. so, package model contain databases access or web service requests example, , view package contain servlet pages , user interactions programme. when user select option, information stored in public static var other classes can access. conflicts occur when 2 users select 2 different options. cause variable static... need variable can shared between classes , keep value given user

your first option listed, using static variables store mutable state, doesn't work in java ee web application. you're right different users on same server see same values. in addition that, if multiple instances of application deployed, static variables on 1 server can contain different values contain on server, if have several boxes, each instance of web application, , load balancer in front of them sending requests round-robin, users can see different state different requests. can exclude option.

your second option, "(2) using setter , getter", isn't clear @ all.

if want keep state visible 1 user, can put in httpsession. things stored in httpsession specific logged-in current user. temporary storage spot only, when session times out contents of session go away. putting many things in httpsession can problem; can take lot of memory, , can cause slowdowns in clustered application nodes try copy information between nodes keep every server's session contents current. (an alternative pinning users given server result load distributed unevenly.)

it can preferable keep user-specific information in database. way data in 1 place available instances of application, can cache other data database, , application can make sure appropriate user sees it. data persistent instead of going away when session times out.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -