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