Results 1 to 2 of 2
  1. #1
    trashhead is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default Static variables in servlets

    I have a servlet. And in this servlet I have a helper-class that makes all the communications to the backend. Communications to the backend goes through an enterprise bean which is created in the constructor of the helperClass.

    What I would like to do is to only run the constructor once for every servlet-request. And after that just access my helperClass in a static way.

    So for example

    in Servlet-class i write:
    new HelperClass();

    and in my other classes i just write:
    HelperClass.getDataA()
    HelperClass.getDataB()

    etc etc.

    The enterprise java bean object in the HelperClass is static(lets call in eeObject).

    What happens when I do all this is that my eeObject is initialized when accessed for the first time in the constructor. But for every request after that it is never reset. I can even log out and log in with a different user and the eeObject is still the same as when initialized.

    So my question is:
    How do I get my HelperClass to be unique for every user(or the eeObject). And I would only like to initialize it either once per servlet-request or once per user-session. And at the same time i would also like to be able to access it in a static way.

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by trashhead View Post
    So my question is:
    How do I get my HelperClass to be unique for every user(or the eeObject). And I would only like to initialize it either once per servlet-request or once per user-session. And at the same time i would also like to be able to access it in a static way.
    Create a Session object for each user; after you have done so create a single HelperClass object and stick it in your Session object. You can access it through your Session object but not 'in a static way'.

    kind regards,

    Jos

Similar Threads

  1. can we synchronise static variables
    By sandeepsai39 in forum New To Java
    Replies: 4
    Last Post: 09-02-2010, 01:25 PM
  2. Replies: 6
    Last Post: 02-15-2010, 04:31 AM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  4. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:08 PM
  5. Help with static variables
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 05:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •