01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest.webapp.servlets;
05:
06: import java.io.IOException;
07: import java.io.PrintWriter;
08:
09: import javax.servlet.http.HttpServlet;
10: import javax.servlet.http.HttpServletRequest;
11: import javax.servlet.http.HttpServletResponse;
12:
13: public class RootCounterServlet extends HttpServlet {
14: private final RootCounterServlet.Counter counterObject = new Counter();
15:
16: private static class Counter {
17: private int counter;
18:
19: public Counter() {
20: counter = 0;
21: }
22:
23: public synchronized void increment() {
24: counter++;
25: }
26:
27: public synchronized void setValue(int newValue) {
28: counter = newValue;
29: }
30:
31: public synchronized int getValue() {
32: return counter;
33: }
34: }
35:
36: private int getCurrentCountValue() {
37: counterObject.increment();
38: return counterObject.getValue();
39: }
40:
41: public void doGet(HttpServletRequest request,
42: HttpServletResponse response) throws IOException {
43: response.setContentType("text/plain");
44: PrintWriter out = response.getWriter();
45: out.println(getCurrentCountValue());
46: }
47: }
|