1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.server.session;
20
21 import java.io.IOException;
22 import java.io.Serializable;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession;
29
30 import org.junit.Assert;
31
32
33
34
35
36
37
38 public class WebAppObjectInSessionServlet extends HttpServlet
39 {
40 @Override
41 protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
42 {
43 try
44 {
45 String action = request.getParameter("action");
46 if ("set".equals(action))
47 {
48 HttpSession session = request.getSession(true);
49 session.setAttribute("staticAttribute", new TestSharedStatic());
50
51 Object staticAttribute = session.getAttribute("staticAttribute");
52 Assert.assertTrue(staticAttribute instanceof TestSharedStatic);
53
54
55
56
57
58
59
60 }
61 else if ("get".equals(action))
62 {
63 HttpSession session = request.getSession(false);
64 Object staticAttribute = session.getAttribute("staticAttribute");
65 System.err.println("staticAttribute="+staticAttribute);
66 Assert.assertTrue(staticAttribute instanceof TestSharedStatic);
67
68
69
70
71
72
73 }
74 }
75 catch (Exception e)
76 {
77
78 httpServletResponse.sendError(500,e.toString());
79 throw new ServletException(e);
80 }
81 }
82
83
84
85 public class TestSharedNonStatic implements Serializable
86 {
87 }
88
89
90 public static class TestSharedStatic implements Serializable
91 {
92 }
93 }