Results 1 to 2 of 2
- 10-16-2010, 06:18 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 1
- Rep Power
- 0
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.
- 10-16-2010, 07:12 PM #2
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
-
non-static variable cannot be referenced from a static context
By keo in forum New To JavaReplies: 5Last Post: 10-15-2010, 04:21 AM -
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:59 AM -
static variable
By udhayageetha in forum AWT / SwingReplies: 17Last Post: 07-07-2008, 11:22 AM -
About static variable
By MarkWilson in forum New To JavaReplies: 5Last Post: 06-27-2008, 01:43 PM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks