1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import javax.servlet.*; 22 import javax.servlet.http.*; 23 24 33 34 public class Session04 extends HttpServlet { 35 36 public void doGet(HttpServletRequest request, HttpServletResponse response) 37 throws IOException, ServletException { 38 39 log("Session04 - Starting, requestedSessionId = " + 40 request.getRequestedSessionId()); 41 response.setContentType("text/plain"); 42 PrintWriter writer = response.getWriter(); 43 44 StringBuffer results = new StringBuffer (); 46 HttpSession oldSession = request.getSession(false); 47 if (oldSession == null) 48 results.append(" No existing session/"); 49 50 String oldSessionId = null; 52 if (oldSession != null) { 53 try { 54 oldSessionId = oldSession.getId(); 55 } catch (IllegalStateException e) { 56 results.append(" Old session is expired/"); 57 } 58 } 59 60 String requestedSessionId = null; 62 if (oldSessionId != null) { 63 requestedSessionId = request.getRequestedSessionId(); 64 if (requestedSessionId == null) { 65 results.append(" No requested session id/"); 66 } else { 67 if (!request.isRequestedSessionIdValid()) 68 results.append(" Requested session id is not valid/"); 69 if (!oldSessionId.equals(requestedSessionId)) { 70 results.append(" Requested session="); 71 results.append(requestedSessionId); 72 results.append(" Old session="); 73 results.append(oldSessionId); 74 results.append("/"); 75 } 76 } 77 } 78 79 if (requestedSessionId != null) { 81 if (!request.isRequestedSessionIdFromCookie()) 82 results.append(" Requested session not from cookie/"); 83 if (request.isRequestedSessionIdFromURL()) 84 results.append(" Requested session from URL/"); 85 } 86 87 if (oldSession != null) { 89 SessionBean bean = new SessionBean(); 90 bean.setStringProperty("Session04"); 91 oldSession.setAttribute("sessionBean", bean); 92 } 93 94 if (oldSession != null) { 96 try { 97 oldSession.invalidate(); 98 } catch (IllegalStateException e) { 99 results.append(" Old session is already invalidated/"); 100 } 101 } 102 103 HttpSession newSession = request.getSession(true); 105 if (newSession == null) { 106 results.append(" Cannot create new session/"); 107 } else { 108 String newSessionId = null; 109 try { 110 newSessionId = newSession.getId(); 111 } catch (IllegalStateException e) { 112 results.append(" New session is already invalidated/"); 113 } 114 if ((oldSession != null) && (newSession != null)) { 115 if (oldSession == newSession) 116 results.append(" oldSession == newSession/"); 117 if (oldSession.equals(newSession)) 118 results.append(" oldSession equals newSession/"); 119 } 120 if ((oldSessionId != null) && (newSessionId != null) && 121 oldSessionId.equals(newSessionId)) { 122 results.append(" New session id = old session id/"); 123 } 124 } 125 126 if (newSession != null) { 128 SessionBean bean = 129 (SessionBean) newSession.getAttribute("sessionBean"); 130 if (bean != null) 131 results.append(" New session has attribute already/"); 132 } 133 134 newSession.setAttribute("activationListener", 136 new SessionListener03()); 137 138 if (results.length() == 0) 140 writer.println("Session04 PASSED"); 141 else { 142 writer.print("Session04 FAILED -"); 143 writer.println(results.toString()); 144 } 145 while (true) { 146 String message = StaticLogger.read(); 147 if (message == null) 148 break; 149 writer.println(message); 150 } 151 StaticLogger.reset(); 152 log("Session04 - Stopping"); 153 154 } 155 156 } 157 | Popular Tags |