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.direct.*;
024    import org.apache.geronimo.samples.daytrader.util.*;
025    
026    import org.apache.geronimo.samples.daytrader.*;
027    
028    /**
029     * 
030     * PingJDBCReadPrepStmt uses a prepared statement for database read access. 
031     * This primative uses {@link org.apache.geronimo.samples.daytrader.direct.TradeDirect} to set the price of a random stock
032     * (generated by {@link org.apache.geronimo.samples.daytrader.Trade_Config}) through the use of prepared statements. 
033     * 
034     */
035    
036    public class PingJDBCRead extends HttpServlet
037    {
038    
039            private static String initTime;
040            private static int hitCount;
041    
042            /**
043             * forwards post requests to the doGet method
044             * Creation date: (11/6/2000 10:52:39 AM)
045             * @param res javax.servlet.http.HttpServletRequest
046             * @param res2 javax.servlet.http.HttpServletResponse
047             */
048            public void doPost(HttpServletRequest req, HttpServletResponse res)
049                    throws ServletException, IOException
050            {
051                    doGet(req, res);
052            }
053            /**
054            * this is the main method of the servlet that will service all get requests.
055            * @param request HttpServletRequest
056            * @param responce HttpServletResponce
057            **/
058            public void doGet(HttpServletRequest req, HttpServletResponse res)
059                    throws ServletException, IOException
060            {
061                    res.setContentType("text/html");
062                    java.io.PrintWriter out = res.getWriter();
063                    String symbol=null;
064                    StringBuffer output = new StringBuffer(100);
065    
066                    try
067                            {
068                            //TradeJDBC uses prepared statements so I am going to make use of it's code.
069                            TradeDirect trade = new TradeDirect();
070                            symbol = TradeConfig.rndSymbol();
071                            
072                            QuoteDataBean quoteData = null;
073                            int iter = TradeConfig.getPrimIterations();
074                            for (int ii = 0; ii < iter; ii++) {
075                                    quoteData = trade.getQuote(symbol);
076                            }
077    
078                            output.append(
079                                    "<html><head><title>Ping JDBC Read w/ Prepared Stmt.</title></head>"
080                                            + "<body><HR><FONT size=\"+2\" color=\"#000066\">Ping JDBC Read w/ Prep Stmt:</FONT><HR><FONT size=\"-1\" color=\"#000066\">Init time : "
081                                            + initTime);
082                            hitCount++;
083                            output.append("<BR>Hit Count: " + hitCount);
084                            output.append(
085                                    "<HR>Quote Information <BR><BR>: "
086                                            + quoteData.toHTML());
087                            output.append("<HR></body></html>");
088                            out.println(output.toString());
089                    }
090                    catch (Exception e)
091                    {
092                            Log.error(
093                                    e,
094                                    "PingJDBCRead w/ Prep Stmt -- error getting quote for symbol",
095                                    symbol);
096                            res.sendError(500, "PingJDBCRead Exception caught: " + e.toString());
097                    }
098    
099            }
100            /** 
101             * returns a string of information about the servlet
102             * @return info String: contains info about the servlet
103             **/
104            public String getServletInfo()
105            {
106                    return "Basic JDBC Read using a prepared statment, makes use of TradeJDBC class";
107            }
108            /**
109            * called when the class is loaded to initialize the servlet
110            * @param config ServletConfig:
111            **/
112            public void init(ServletConfig config) throws ServletException
113            {
114                    super.init(config);
115                    hitCount = 0;
116                    initTime = new java.util.Date().toString();
117            }
118    }