FileDocCategorySizeDatePackage
CalculatorActionServlet.javaAPI DocJBoss 4.2.13511Fri Jul 13 20:54:56 BST 2007org.jboss.tutorial.ear.servlet

CalculatorActionServlet

public class CalculatorActionServlet extends HttpServlet
comment
author
Bill Burke

Fields Summary
private org.jboss.tutorial.ear.bean.CalculatorLocal
calculator
Constructors Summary
Methods Summary
protected voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

      doit(req, resp);
   
protected voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

      doit(req, resp);
   
protected voiddoit(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

      int op1 = Integer.parseInt(req.getParameter("op1"));
      int op2 = Integer.parseInt(req.getParameter("op2"));

      String action = req.getParameter("action");

      int result = 0;
      if (action.equals("Add"))
         result = calculator.add(op1, op2);
      else if (action.equals("Subtract")) result = calculator.subtract(op1, op2);

      resp.setContentType("text/html");
      PrintWriter writer = resp.getWriter();
      writer.println("<html>");
      writer.println("<body>");
      writer.println("<h1>Answer: " + result + "</h1>");
      writer.println("</body");
      writer.println("</html>");
   
public voidinit()
Simulate injection of Calculator EJB

throws
ServletException

      super.init();
      try
      {
         InitialContext ctx = new InitialContext();

         // J2EE 1.5 has not yet defined exact XML <ejb-ref> syntax for EJB3
         CalculatorLocal calculator = (CalculatorLocal) ctx.lookup("tutorial/calculator/local");
         setCalculator(calculator);
      }
      catch (NamingException e)
      {
         throw new RuntimeException(e);
      }

   
public voidsetCalculator(org.jboss.tutorial.ear.bean.CalculatorLocal calculator)
The @EJB annotation is similated. Tomcat does not yet understand injection annotations.

param
calculator

      this.calculator = calculator;