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 * 028 * PingServlet tests fundamental dynamic HTML creation functionality through 029 * server side servlet processing. 030 * 031 */ 032 public class PingServlet extends HttpServlet 033 { 034 035 private static String initTime; 036 private static int hitCount; 037 038 /** 039 * forwards post requests to the doGet method 040 * Creation date: (11/6/2000 10:52:39 AM) 041 * @param res javax.servlet.http.HttpServletRequest 042 * @param res2 javax.servlet.http.HttpServletResponse 043 */ 044 public void doPost(HttpServletRequest req, HttpServletResponse res) 045 throws ServletException, IOException 046 { 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 req, HttpServletResponse res) 055 throws ServletException, IOException 056 { 057 try 058 { 059 res.setContentType("text/html"); 060 061 // The following 2 lines are the difference between PingServlet and PingServletWriter 062 // the latter uses a PrintWriter for output versus a binary output stream. 063 ServletOutputStream out = res.getOutputStream(); 064 //java.io.PrintWriter out = res.getWriter(); 065 hitCount++; 066 out.println( 067 "<html><head><title>Ping Servlet</title></head>" 068 + "<body><HR><BR><FONT size=\"+2\" color=\"#000066\">Ping Servlet<BR></FONT><FONT size=\"+1\" color=\"#000066\">Init time : " 069 + initTime 070 + "<BR><BR></FONT> <B>Hit Count: " 071 + hitCount 072 + "</B></body></html>"); 073 } 074 catch (Exception e) 075 { 076 Log.error(e, "PingServlet.doGet(...): general exception caught"); 077 res.sendError(500, e.toString()); 078 079 } 080 } 081 /** 082 * returns a string of information about the servlet 083 * @return info String: contains info about the servlet 084 **/ 085 public String getServletInfo() 086 { 087 return "Basic dynamic HTML generation through a servlet"; 088 } 089 /** 090 * called when the class is loaded to initialize the servlet 091 * @param config ServletConfig: 092 **/ 093 public void init(ServletConfig config) throws ServletException 094 { 095 super.init(config); 096 initTime = new java.util.Date().toString(); 097 hitCount = 0; 098 099 } 100 }