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 * PingHTTPSession2 session create/destroy further extends the previous test by 028 * invalidating the HTTP Session on every 5th user access. This results in testing 029 * HTTPSession create and destroy 030 * 031 */ 032 public class PingSession2 extends HttpServlet { 033 034 private static String initTime; 035 private static int hitCount; 036 037 /** 038 * forwards post requests to the doGet method 039 * Creation date: (11/6/2000 10:52:39 AM) 040 * @param res javax.servlet.http.HttpServletRequest 041 * @param res2 javax.servlet.http.HttpServletResponse 042 */ 043 public void doPost(HttpServletRequest req, HttpServletResponse res) 044 throws ServletException, IOException { 045 doGet(req, res); 046 } 047 /** 048 * this is the main method of the servlet that will service all get requests. 049 * @param request HttpServletRequest 050 * @param responce HttpServletResponce 051 **/ 052 public void doGet(HttpServletRequest request, HttpServletResponse response) 053 throws ServletException, IOException { 054 HttpSession session = null; 055 try 056 { 057 try 058 { 059 session = request.getSession(true); 060 } 061 catch (Exception e) 062 { 063 Log.error(e, "PingSession2.doGet(...): error getting session"); 064 //rethrow the exception for handling in one place. 065 throw e; 066 067 } 068 069 // Get the session data value 070 Integer ival = (Integer) session.getAttribute("sessiontest.counter"); 071 //if there is not a counter then create one. 072 if (ival == null) 073 { 074 ival = new Integer(1); 075 } 076 else 077 { 078 ival = new Integer(ival.intValue() + 1); 079 } 080 session.setAttribute("sessiontest.counter", ival); 081 //if the session count is equal to five invalidate the session 082 if (ival.intValue() == 5) 083 { 084 session.invalidate(); 085 } 086 087 try 088 { 089 // Output the page 090 response.setContentType("text/html"); 091 response.setHeader("SessionTrackingTest-counter", ival.toString()); 092 093 PrintWriter out = response.getWriter(); 094 out.println( 095 "<html><head><title>Session Tracking Test 2</title></head><body><HR><BR><FONT size=\"+2\" color=\"#000066\">HTTP Session Test 2: Session create/invalidate <BR></FONT><FONT size=\"+1\" color=\"#000066\">Init time: " 096 + initTime 097 + "</FONT><BR><BR>"); 098 hitCount++; 099 out.println( 100 "<B>Hit Count: " 101 + hitCount 102 + "<BR>Session hits: " 103 + ival 104 + "</B></body></html>"); 105 } 106 catch (Exception e) 107 { 108 Log.error(e, "PingSession2.doGet(...): error getting session information"); 109 //rethrow the exception for handling in one place. 110 throw e; 111 } 112 113 } 114 115 catch (Exception e) 116 { 117 //log the excecption 118 Log.error(e, "PingSession2.doGet(...): error."); 119 //set the server responce to 500 and forward to the web app defined error page 120 response.sendError( 121 500, 122 "PingSession2.doGet(...): error. " + e.toString()); 123 } 124 } //end of the method 125 /** 126 * returns a string of information about the servlet 127 * @return info String: contains info about the servlet 128 **/ 129 public String getServletInfo() 130 { 131 return "HTTP Session Key: Tests management of a read/write unique id"; 132 } 133 /** 134 * called when the class is loaded to initialize the servlet 135 * @param config ServletConfig: 136 **/ 137 public void init(ServletConfig config) throws ServletException { 138 super.init(config); 139 hitCount = 0; 140 initTime = new java.util.Date().toString(); 141 142 } 143 }