001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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.acme;
018    
019    import java.io.IOException;
020    import java.io.PrintWriter;
021    
022    import javax.ejb.CreateException;
023    import javax.naming.Context;
024    import javax.naming.InitialContext;
025    import javax.naming.NamingException;
026    import javax.servlet.ServletException;
027    import javax.servlet.http.HttpServlet;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * Run this servlet
033     *
034     * @version $Rev: 411947 $ $Date: 2006-06-05 16:26:17 -0700 (Mon, 05 Jun 2006) $
035     */
036    public class MagicGBallServlet extends HttpServlet {
037    
038            public MagicGBallServlet() {
039                    super();
040            }
041    
042            protected void service(HttpServletRequest req, HttpServletResponse res)
043                    throws ServletException, IOException {
044                    try {
045                            Context ctx = new InitialContext();
046                            MagicGBallLocalHome ejbHome = (MagicGBallLocalHome) ctx.lookup("java:comp/env/mGball");
047                            MagicGBallLocal m8ball = ejbHome.create();
048                            String question = req.getParameter("question");
049                            String answer = m8ball.ask(question);
050                            
051                            PrintWriter out = res.getWriter();
052                            out.print(answer);
053                    } catch (NamingException e) {
054                            e.printStackTrace();
055                    } catch (CreateException e) {
056                            e.printStackTrace();
057                    } catch (IOException e) {
058                            e.printStackTrace();
059                    }
060            }
061    
062    }