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

    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.


  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


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