Wiki Markup |
---|
{scrollbar} |
Sample of a Stateless Session Bean in EJB 3.0
This sample will demonstrate the following new features from EJB 3.0
- Elimination of the requirement for EJB component interfaces for session beans. The required business interface for a session bean can be a plain Java interface rather than an EJBObject, EJBLocalObject, or java.rmi.Remote interface.
- Elimination of the requirement for home interfaces for session beans.
- Encapsulation of environmental dependencies and JNDI access through the use of annotations, dependency injection mechanisms, and simple lookup mechanisms.
- Introduction of Java metadata annotations to be used as an alternative to deployment descriptors.
Calculator Implementation
Calculator.java: A stateless session bean that implements a simple java interface instead of an EJB component interface like EJBObject, EJBLocalObject or java.rmi.Remote. By annotating this class as a @Stateless session there is no need for a deployment descriptor to describe it separately. This class implements both a local and remote business interface, namely CalculatorLocal and CalculatorRemote.
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
package org.apache.geronimo.samples.calculator; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.geronimo.samples.slsb.calculator.CalculatorLocal; public class CalculatorServlet extends HttpServlet { @EJB private CalculatorLocal calc = null; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { String firstNumber = req.getParameter("firstNumber"); String secondNumber = req.getParameter("secondNumber"); String operation = req.getParameter("operation"); int firstInt = (firstNumber == null) ? 0 : Integer.valueOf(firstNumber).intValue(); int secondInt = (secondNumber == null) ? 0 : Integer.valueOf(secondNumber).intValue(); if ( "multiply".equals(operation) ) { req.setAttribute("result", calc.multiply(firstInt, secondInt)); } else if ( "add".equals(operation) ) { req.setAttribute("result", calc.sum(firstInt, secondInt)); } System.out.println("Result is " + req.getAttribute("result")); getServletContext().getRequestDispatcher("/sample-docu.jsp").forward(req, resp); } catch ( Exception e ) { e.printStackTrace(); throw new ServletException(e); } } } |
Deployment Plans
The structure of the deployable should look like the following:
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://geronimo.apache.org/xml/ns/j2ee/application-1.2"> <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2"> <dep:moduleId> <dep:groupId>org.apache.geronimo.samples</dep:groupId> <dep:artifactId>calculator-stateless-jetty</dep:artifactId> <dep:version>2.1.2-SNAPSHOT<2</dep:version> <dep:type>car</dep:type> </dep:moduleId> <dep:dependencies> <dep:dependency> <dep:groupId>org.apache.geronimo.configs</dep:groupId> <dep:artifactId>jasper<artifactId>jetty6</dep:artifactId> <dep:version>2.2-SNAPSHOT<1.2</dep:version> <dep:type>car</dep:type> </dep:dependency> <dep:dependency> <dep:groupId>org.apache.geronimo.configs</dep:groupId> <dep:artifactId>jetty6<artifactId>jasper</dep:artifactId> <dep:version>2.2-SNAPSHOT<1.2</dep:version> <dep:type>car</dep:type> </dep:dependency> <dep:dependency> <dep:groupId>org.apache.geronimo.configs</dep:groupId> <dep:artifactId>openjpa<artifactId>openejb</dep:artifactId> <dep:version>2.1.2-SNAPSHOT<2</dep:version> <dep:type>car</dep:type> </dep:dependency> <dep:dependency> <dep:groupId>org.apache.geronimo.configs</dep:groupId> <dep:artifactId>openejb<artifactId>openjpa</dep:artifactId> <dep:version>2.1.2-SNAPSHOT<2</dep:version> <dep:type>car</dep:type> </dep:dependency> </dep:dependencies> <dep:hidden-classes/> <dep:non-overridable-classes/> </dep:environment> </application> |