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: import javax.servlet.http.HttpSession;
13:
14: public class CounterServlet extends HttpServlet {
15:
16: public void doGet(HttpServletRequest request,
17: HttpServletResponse response) throws IOException {
18: response.setContentType("text/html");
19: PrintWriter out = response.getWriter();
20: try {
21: HttpSession session = request.getSession(true);
22:
23: Integer count = (Integer) session.getAttribute("count");
24: if (count == null) {
25: count = new Integer(0);
26: }
27:
28: if (request.getParameter("read") != null) {
29: if (session.isNew()) {
30: out.println("session is new"); // this is an error condition (client will fail trying to parse this as int)
31: } else {
32: out.println(count.intValue());
33: }
34: } else {
35: int newValue = count.intValue() + 1;
36: session.setAttribute("count", new Integer(newValue));
37: out.println(newValue);
38: }
39: } catch (RuntimeException e) {
40: e.printStackTrace(out);
41: throw e;
42: }
43: }
44: }
|