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 * PingHTTPSession1 - SessionID tests fundamental HTTP session functionality 028 * by creating a unique session ID for each individual user. The ID is stored 029 * in the users session and is accessed and displayed on each user request. 030 * 031 */ 032 public class PingSession1 extends HttpServlet { 033 private static int count; 034 // For each new session created, add a session ID of the form "sessionID:" + count 035 private static String initTime; 036 private static int hitCount; 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 //get the users session, if the user does not have a session create one. 060 session = request.getSession(true); 061 } 062 catch (Exception e) 063 { 064 Log.error(e, "PingSession1.doGet(...): error getting session"); 065 //rethrow the exception for handling in one place. 066 throw e; 067 } 068 069 // Get the session data value 070 Integer ival = (Integer) session.getAttribute("sessiontest.counter"); 071 //if their is not a counter create one. 072 if (ival == null) 073 { 074 ival = new Integer(count++); 075 session.setAttribute("sessiontest.counter", ival); 076 } 077 String SessionID = "SessionID:" + ival.toString(); 078 079 // Output the page 080 response.setContentType("text/html"); 081 response.setHeader("SessionKeyTest-SessionID", SessionID); 082 083 PrintWriter out = response.getWriter(); 084 out.println( 085 "<html><head><title>HTTP Session Key Test</title></head><body><HR><BR><FONT size=\"+2\" color=\"#000066\">HTTP Session Test 1: Session Key<BR></FONT><FONT size=\"+1\" color=\"#000066\">Init time: " 086 + initTime 087 + "</FONT><BR><BR>"); 088 hitCount++; 089 out.println( 090 "<B>Hit Count: " 091 + hitCount 092 + "<BR>Your HTTP Session key is " 093 + SessionID 094 + "</B></body></html>"); 095 } 096 catch (Exception e) 097 { 098 //log the excecption 099 Log.error(e, "PingSession1.doGet(..l.): error."); 100 //set the server responce to 500 and forward to the web app defined error page 101 response.sendError( 102 500, 103 "PingSession1.doGet(...): error. " + e.toString()); 104 } 105 } 106 /** 107 * returns a string of information about the servlet 108 * @return info String: contains info about the servlet 109 **/ 110 111 public String getServletInfo() 112 { 113 return "HTTP Session Key: Tests management of a read only unique id"; 114 } 115 /** 116 * called when the class is loaded to initialize the servlet 117 * @param config ServletConfig: 118 **/ 119 public void init(ServletConfig config) throws ServletException { 120 super.init(config); 121 count = 0; 122 hitCount = 0; 123 initTime = new java.util.Date().toString(); 124 125 } 126 }