1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import javax.servlet.*; 22 import javax.servlet.http.*; 23 import org.apache.tester.shared.SharedSessionBean; 24 import org.apache.tester.unpshared.UnpSharedSessionBean; 25 import org.apache.tester.unshared.UnsharedSessionBean; 26 27 28 36 37 public class Session01 extends HttpServlet { 38 39 public void doGet(HttpServletRequest request, HttpServletResponse response) 40 throws IOException, ServletException { 41 42 response.setContentType("text/plain"); 43 PrintWriter writer = response.getWriter(); 44 45 boolean ok = true; 47 HttpSession session = request.getSession(false); 48 if (session != null) { 49 writer.println("Session01 FAILED - Requested existing session " + 50 session.getId()); 51 ok = false; 52 } 53 54 if (ok) { 56 session = request.getSession(true); 57 if (session == null) { 58 writer.println("Session01 FAILED - No session created"); 59 ok = false; 60 } 61 } 62 63 if (ok) { 65 session.setAttribute("activationListener", 66 new SessionListener03()); 67 } 68 69 if (ok) { 71 if (session.getAttribute("sessionBean") != null) { 72 writer.println("Session01 FAILED - Attribute already exists"); 73 ok = false; 74 } 75 } 76 77 if (ok) { 79 SessionBean bean = new SessionBean(); 80 bean.setStringProperty("Session01"); 81 session.setAttribute("sessionBean", bean); 82 } 83 84 if (ok) { 86 Object bean = session.getAttribute("sessionBean"); 87 if (bean == null) { 88 writer.println("Session01 FAILED - Cannot retrieve attribute"); 89 ok = false; 90 } else if (!(bean instanceof SessionBean)) { 91 writer.println("Session01 FAILED - Attribute instance of " + 92 bean.getClass().getName()); 93 ok = false; 94 } else { 95 String value = ((SessionBean) bean).getStringProperty(); 96 if (!"Session01".equals(value)) { 97 writer.println("Session01 FAILED - Property = " + value); 98 ok = false; 99 } 100 } 101 } 102 103 if (ok) { 105 session.setAttribute("FOO", "BAR"); 106 session.setAttribute("FOO", null); 107 if (session.getAttribute("FOO") != null) { 108 writer.println("Session01 FAILED - setAttribute(name,null)"); 109 ok = false; 110 } 111 } 112 113 if (ok) { 115 SharedSessionBean ssb = new SharedSessionBean(); 116 ssb.setStringProperty("Session01"); 117 session.setAttribute("sharedSessionBean", ssb); 118 UnpSharedSessionBean ussb = new UnpSharedSessionBean(); 119 ussb.setStringProperty("Session01"); 120 session.setAttribute("unpSharedSessionBean", ussb); 121 UnsharedSessionBean usb = new UnsharedSessionBean(); 122 usb.setStringProperty("Session01"); 123 session.setAttribute("unsharedSessionBean", usb); 124 } 125 126 if (ok) 128 writer.println("Session01 PASSED"); 129 while (true) { 130 String message = StaticLogger.read(); 131 if (message == null) 132 break; 133 writer.println(message); 134 } 135 StaticLogger.reset(); 136 137 } 138 139 } 140 | Popular Tags |