KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > ear > servlet > CalculatorActionServlet


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.tutorial.ear.servlet;
8
9 import org.jboss.tutorial.ear.bean.CalculatorLocal;
10
11 import javax.annotation.EJB;
12 import javax.naming.InitialContext JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServlet JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18 import javax.annotation.EJB;
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 /**
23  * comment
24  *
25  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
26  */

27 public class CalculatorActionServlet extends HttpServlet JavaDoc
28 {
29    protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
30            throws ServletException JavaDoc, IOException JavaDoc
31    {
32       doit(req, resp);
33    }
34
35    protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
36            throws ServletException JavaDoc, IOException JavaDoc
37    {
38       doit(req, resp);
39    }
40
41    /**
42     * Simulate injection of Calculator EJB
43     *
44     * @throws ServletException
45     */

46    public void init() throws ServletException JavaDoc
47    {
48       super.init();
49       try
50       {
51          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
52
53          // J2EE 1.5 has not yet defined exact XML <ejb-ref> syntax for EJB3
54
CalculatorLocal calculator = (CalculatorLocal) ctx.lookup(CalculatorLocal.class.getName());
55          setCalculator(calculator);
56       }
57       catch (NamingException JavaDoc e)
58       {
59          throw new RuntimeException JavaDoc(e);
60       }
61
62    }
63
64    private CalculatorLocal calculator;
65
66    /**
67     * The @EJB annotation is similated. Tomcat does not yet understand injection annotations.
68     * @param calculator
69     */

70    @EJB(name = "calculator")
71    public void setCalculator(CalculatorLocal calculator)
72    {
73       this.calculator = calculator;
74    }
75
76    protected void doit(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
77            throws ServletException JavaDoc, IOException JavaDoc
78    {
79       int op1 = Integer.parseInt(req.getParameter("op1"));
80       int op2 = Integer.parseInt(req.getParameter("op2"));
81
82       String JavaDoc action = req.getParameter("action");
83
84       int result = 0;
85       if (action.equals("Add"))
86          result = calculator.add(op1, op2);
87       else if (action.equals("Subtract")) result = calculator.subtract(op1, op2);
88
89       resp.setContentType("text/html");
90       PrintWriter JavaDoc writer = resp.getWriter();
91       writer.println("<html>");
92       writer.println("<body>");
93       writer.println("<h1>Answer: " + result + "</h1>");
94       writer.println("</body");
95       writer.println("</html>");
96    }
97 }
98
Popular Tags