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

    Default using a static variable in a servlet

    Hi friends,
    I wanted to count the number of object entities that i add through my html page and generate a number(manually) for each object added. After adding each object, i want to have a display of the total number of objects added in the same page with object number concatenated to the object name. i thought of using a static variable inside a class and update that variable whenever i'm calling a method to add the object. am i going in the right way? give me your opinions.

    thanks,
    Rajkumar.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    static properties would be global to the application, for example, if three users each accessed the same page, they all would see the same count value, the sum of all objects each one of them has added.

    I think what you are looking for here is a 'session' variable.

    With the HttpServletRequest object, inside your servlet doGet method, etc.

    Java Code:
    Integer counter = request.getSession().getAttribute("myCounter"); 
    if (counter = null) {
      counter = new Integer(0);
    }
    
    // mutate the counter here
    counter = new Integer(counter.intValue() + 1); // or how ever you compute total objects
    
    request.getSession().setAttribute("myCounter", counter);

Similar Threads

  1. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  2. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  3. static variable
    By udhayageetha in forum AWT / Swing
    Replies: 17
    Last Post: 07-07-2008, 11:22 AM
  4. About static variable
    By MarkWilson in forum New To Java
    Replies: 5
    Last Post: 06-27-2008, 01:43 PM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •