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 final class NewSessionAfterInvalidateTestServlet extends
15: HttpServlet {
16:
17: public void doGet(HttpServletRequest request,
18: HttpServletResponse response) throws IOException {
19: HttpSession session = request.getSession(true);
20: PrintWriter out = response.getWriter();
21:
22: int step = Integer.parseInt(request.getParameter("step"));
23:
24: if (step == 2) {
25: String val = (String) session.getAttribute("key");
26: // check that session was re-joined
27: if (!"value2".equals(val)) {
28: out.println("existing session has bad data: " + val);
29: return;
30: }
31: }
32:
33: String firstId = session.getId();
34: response.setContentType("text/html");
35:
36: session.setAttribute("key", "value");
37: session.invalidate();
38:
39: HttpSession newSession = request.getSession(false);
40:
41: if (newSession != null) {
42: out
43: .println("getSession(false) returned a session after invalidate()");
44: return;
45: }
46:
47: newSession = request.getSession(true);
48:
49: if (newSession == session) {
50: out.println("same session object returned");
51: return;
52: }
53:
54: if (newSession.getId().equals(firstId)) {
55: out.println("session id resused for new session");
56: return;
57: }
58:
59: // test writing to the new session (would fail if lock not acquired)
60: newSession.setAttribute("key", "value2");
61:
62: String value = (String) newSession.getAttribute("key");
63: if ("value2".equals(value)) {
64: out.println("OK");
65: } else {
66: out.println("Session has wrong value: " + value);
67: }
68: }
69:
70: }
|