View Javadoc

1   // ========================================================================
2   // Copyright 2004-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
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  
26  /**
27   * WebAppObjectInSessionServlet
28   *
29   *
30   */
31  public class WebAppObjectInSessionServlet extends HttpServlet
32  {
33      @Override
34      protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException
35      {
36          try
37          {
38              String action = request.getParameter("action");
39              if ("set".equals(action))
40              {
41                  HttpSession session = request.getSession(true);
42                  session.setAttribute("staticAttribute", new TestSharedStatic());
43  //                session.setAttribute("objectAttribute", new TestSharedNonStatic());
44                  // The session itself is not shareable, since the implementation class
45                  // refers to the session manager via the hidden field this$0, and
46                  // it seems there is no way to mark the hidden field as transient.
47  //                session.setAttribute("sessionAttribute", session);
48              }
49              else if ("get".equals(action))
50              {
51                  HttpSession session = request.getSession(false);
52                  Object staticAttribute = session.getAttribute("staticAttribute");
53                  assert staticAttribute instanceof TestSharedStatic;
54  //                Object objectAttribute = session.getAttribute("objectAttribute");
55  //                assert objectAttribute instanceof TestSharedNonStatic;
56  //                Object sessionAttribute = session.getAttribute("sessionAttribute");
57  //                assert sessionAttribute instanceof HttpSession;
58              }
59          }
60          catch (Exception e)
61          {
62              e.printStackTrace();
63          }
64      }
65  
66      // Non static inner classes are not shareable, because even if this class is portable,
67      // the hidden field this$0 refers to the servlet, which is a non portable class.
68      public class TestSharedNonStatic implements Serializable
69      {
70      }
71  
72      // Static inner classes are shareable
73      public static class TestSharedStatic implements Serializable
74      {
75      }
76  }