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.calculator; 018 019 import java.io.IOException; 020 import javax.ejb.EJB; 021 import javax.servlet.ServletException; 022 import javax.servlet.http.HttpServlet; 023 import javax.servlet.http.HttpServletRequest; 024 import javax.servlet.http.HttpServletResponse; 025 026 import org.apache.geronimo.samples.slsb.calculator.CalculatorLocal; 027 028 /** 029 * A simple servlet that will reference a bean and perform it's operations. 030 * 031 * Note that a stateful session bean must be declared at the type level 032 * whereas a stateless session bean may be declared at any level. 033 * The ejb container will route every request to different bean instances. 034 */ 035 036 // @EJB(name="CalculatorLocal", beanInterface=CalculatorLocal.class) 037 038 public class CalculatorServlet extends HttpServlet { 039 040 // the ejb container will route every request to different bean instances. 041 @EJB 042 private CalculatorLocal calc = null; 043 044 public void doGet(HttpServletRequest req, HttpServletResponse resp) 045 throws ServletException, IOException { 046 047 try { 048 String firstNumber = req.getParameter("firstNumber"); 049 String secondNumber = req.getParameter("secondNumber"); 050 String operation = req.getParameter("operation"); 051 052 int firstInt = (firstNumber == null) ? 0 : Integer.valueOf(firstNumber).intValue(); 053 int secondInt = (secondNumber == null) ? 0 : Integer.valueOf(secondNumber).intValue(); 054 055 if ( "multiply".equals(operation) ) { 056 req.setAttribute("result", calc.multiply(firstInt, secondInt)); 057 } 058 else if ( "add".equals(operation) ) { 059 req.setAttribute("result", calc.sum(firstInt, secondInt)); 060 } 061 062 System.out.println("Result is " + req.getAttribute("result")); 063 064 getServletContext().getRequestDispatcher("/sample-docu.jsp").forward(req, resp); 065 066 } 067 catch ( Exception e ) { 068 e.printStackTrace(); 069 throw new ServletException(e); 070 } 071 } 072 073 }