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 extends PingServlet by using a PrintWriter for formatted 
029     * output vs. the output stream used by {@link PingServlet}.
030     * 
031     */
032    public class PingServletWriter 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 req, HttpServletResponse res)
053                    throws ServletException, IOException {
054                    try
055                    {
056                            res.setContentType("text/html");
057    
058                            // The following 2 lines are the difference between PingServlet and PingServletWriter
059                            //   the latter uses a PrintWriter for output versus a binary output stream.
060                            //ServletOutputStream out = res.getOutputStream();
061                            java.io.PrintWriter out = res.getWriter();
062                            hitCount++;
063                            out.println(
064                                    "<html><head><title>Ping Servlet Writer</title></head>"
065                                            + "<body><HR><BR><FONT size=\"+2\" color=\"#000066\">Ping Servlet Writer:<BR></FONT><FONT size=\"+1\" color=\"#000066\">Init time : "
066                                            + initTime
067                                            + "<BR><BR></FONT>  <B>Hit Count: "
068                                            + hitCount
069                                            + "</B></body></html>"); 
070                    }
071                    catch (Exception e)
072                    {
073                            Log.error(e, "PingServletWriter.doGet(...): general exception caught"); 
074                            res.sendError(500, e.toString());
075                    }
076            }
077            /** 
078             * returns a string of information about the servlet
079             * @return info String: contains info about the servlet
080             **/
081    
082            public String getServletInfo()
083            {
084                    return "Basic dynamic HTML generation through a servlet using a PrintWriter";
085            }
086            /**
087            * called when the class is loaded to initialize the servlet
088            * @param config ServletConfig:
089            **/
090            public void init(ServletConfig config) throws ServletException {
091                    super.init(config);
092                    hitCount = 0;
093                    initTime = new java.util.Date().toString();
094    
095            }
096    }