001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.samples.daytrader.web.prims; 018 019 import java.io.*; 020 import javax.servlet.*; 021 import javax.servlet.http.*; 022 023 import org.apache.geronimo.samples.daytrader.util.*; 024 025 /** 026 * 027 * PingHTTPSession3 tests the servers ability to manage 028 * and persist large HTTPSession data objects. The servlet creates the large custom 029 * java object {@link PingSession3Object}. This large session object is 030 * retrieved and stored to the session on each user request. The default settings 031 * result in approx 2024 bits being retrieved and stored upon each request. 032 * 033 */ 034 public class PingSession3 extends HttpServlet { 035 private static int NUM_OBJECTS = 2; 036 private static String initTime = null; 037 private static int hitCount = 0; 038 039 /** 040 * forwards post requests to the doGet method 041 * Creation date: (11/6/2000 10:52:39 AM) 042 * @param res javax.servlet.http.HttpServletRequest 043 * @param res2 javax.servlet.http.HttpServletResponse 044 */ 045 public void doPost(HttpServletRequest req, HttpServletResponse res) 046 throws ServletException, IOException { 047 doGet(req, res); 048 } 049 /** 050 * this is the main method of the servlet that will service all get requests. 051 * @param request HttpServletRequest 052 * @param responce HttpServletResponce 053 **/ 054 public void doGet(HttpServletRequest request, HttpServletResponse response) 055 throws ServletException, IOException { 056 057 PrintWriter out = response.getWriter(); 058 //Using a StringBuffer to output all at once. 059 StringBuffer outputBuffer = new StringBuffer(); 060 HttpSession session = null; 061 PingSession3Object[] sessionData; 062 response.setContentType("text/html"); 063 064 //this is a general try/catch block. The catch block at the end of this will forward the responce 065 //to an error page if there is an exception 066 try 067 { 068 069 try 070 { 071 session = request.getSession(true); 072 } 073 catch (Exception e) 074 { 075 Log.error(e, "PingSession3.doGet(...): error getting session"); 076 //rethrow the exception for handling in one place. 077 throw e; 078 079 } 080 // Each PingSession3Object in the PingSession3Object array is 1K in size 081 // NUM_OBJECTS sets the size of the array to allocate and thus set the size in KBytes of the session object 082 // NUM_OBJECTS can be initialized by the servlet 083 // Here we check for the request parameter to change the size and invalidate the session if it exists 084 // NOTE: Current user sessions will remain the same (i.e. when NUM_OBJECTS is changed, all user thread must be restarted 085 // for the change to fully take effect 086 087 String num_objects; 088 if ((num_objects = request.getParameter("num_objects")) != null) 089 { 090 //validate input 091 try 092 { 093 int x = Integer.parseInt(num_objects); 094 if (x > 0) 095 { 096 NUM_OBJECTS = x; 097 } 098 } 099 catch (Exception e) 100 { 101 Log.error(e, "PingSession3.doGet(...): input should be an integer, input=" + num_objects); 102 } // revert to current value on exception 103 104 outputBuffer.append( 105 "<html><head> Session object size set to " 106 + NUM_OBJECTS 107 + "K bytes </head><body></body></html>"); 108 if (session != null) 109 session.invalidate(); 110 out.print(outputBuffer.toString()); 111 out.close(); 112 return; 113 } 114 115 // Get the session data value 116 sessionData = 117 (PingSession3Object[]) session.getAttribute("sessiontest.sessionData"); 118 if (sessionData == null) 119 { 120 sessionData = new PingSession3Object[NUM_OBJECTS]; 121 for (int i = 0; i < NUM_OBJECTS; i++) 122 { 123 sessionData[i] = new PingSession3Object(); 124 } 125 } 126 127 session.setAttribute("sessiontest.sessionData", sessionData); 128 129 //Each PingSession3Object is about 1024 bits, there are 8 bits in a byte. 130 int num_bytes = (NUM_OBJECTS*1024)/8; 131 response.setHeader( 132 "SessionTrackingTest-largeSessionData", 133 num_bytes + "bytes"); 134 135 outputBuffer 136 .append("<html><head><title>Session Large Data Test</title></head><body><HR><BR><FONT size=\"+2\" color=\"#000066\">HTTP Session Test 3: Large Data<BR></FONT><FONT size=\"+1\" color=\"#000066\">Init time: ") 137 .append(initTime) 138 .append("</FONT><BR><BR>"); 139 hitCount++; 140 outputBuffer.append("<B>Hit Count: ").append(hitCount).append( 141 "<BR>Session object updated. Session Object size = " 142 + num_bytes 143 + " bytes </B></body></html>"); 144 //output the Buffer to the printWriter. 145 out.println(outputBuffer.toString()); 146 147 } 148 catch (Exception e) 149 { 150 //log the excecption 151 Log.error(e, "PingSession3.doGet(..l.): error."); 152 //set the server responce to 500 and forward to the web app defined error page 153 response.sendError( 154 500, 155 "PingSession3.doGet(...): error. " + e.toString()); } 156 } 157 /** 158 * returns a string of information about the servlet 159 * @return info String: contains info about the servlet 160 **/ 161 public String getServletInfo() 162 { 163 return "HTTP Session Object: Tests management of a large custom session class"; 164 } 165 /** 166 * called when the class is loaded to initialize the servlet 167 * @param config ServletConfig: 168 **/ 169 public void init(ServletConfig config) throws ServletException { 170 super.init(config); 171 hitCount = 0; 172 initTime = new java.util.Date().toString(); 173 174 } 175 }