View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.geronimo.samples.calculator;
18  
19  import java.io.IOException;
20  import javax.ejb.EJB;
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServlet;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.geronimo.samples.slsb.calculator.CalculatorLocal;
27  
28  /**
29   * A simple servlet that will reference a bean and perform it's operations.
30   * 
31   * Note that a stateful session bean must be declared at the type level
32   * whereas a stateless session bean may be declared at any level.
33   * The ejb container will route every request to different bean instances.
34   */
35  
36   // @EJB(name="CalculatorLocal", beanInterface=CalculatorLocal.class)
37  
38  public class CalculatorServlet extends HttpServlet {
39  
40      // the ejb container will route every request to different bean instances. 
41      @EJB
42      private CalculatorLocal calc = null;
43  
44      public void doGet(HttpServletRequest req, HttpServletResponse resp)
45      throws ServletException, IOException {
46  
47          try {
48              String firstNumber = req.getParameter("firstNumber");
49              String secondNumber = req.getParameter("secondNumber");
50              String operation = req.getParameter("operation");
51  
52              int firstInt = (firstNumber == null) ? 0 : Integer.valueOf(firstNumber).intValue();
53              int secondInt = (secondNumber == null) ? 0 : Integer.valueOf(secondNumber).intValue();
54  
55              if ( "multiply".equals(operation) ) {
56                  req.setAttribute("result", calc.multiply(firstInt, secondInt));
57              }
58              else if ( "add".equals(operation) ) {
59                  req.setAttribute("result", calc.sum(firstInt, secondInt));
60              }
61  
62              System.out.println("Result is " + req.getAttribute("result"));
63  
64              getServletContext().getRequestDispatcher("/sample-docu.jsp").forward(req, resp);
65  
66          }
67          catch ( Exception e ) {
68              e.printStackTrace();
69              throw new ServletException(e);
70          }
71      }
72  
73  }