Home > Documentation > Developer's guide > Tutorials > Web applications > Web Application for EJB access |
To run this tutorial, as a minimum you will be required to have installed the following prerequisite software:
Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:
The application development will take you through the following:
package ejb; import java.math.BigDecimal; import javax.ejb.Remote; @Remote public interface Converter { public BigDecimal dollarToRupees(BigDecimal dollars); public BigDecimal rupeesToEuro(BigDecimal rupees); }
package ejb; import java.math.BigDecimal; import javax.ejb.*; @Stateless public class ConverterBean implements Converter { private BigDecimal rupeeRate = new BigDecimal("40.58"); private BigDecimal euroRate = new BigDecimal("0.018368"); public BigDecimal dollarToRupees(BigDecimal dollars) { BigDecimal result = dollars.multiply(rupeeRate); return result.setScale(2, BigDecimal.ROUND_UP); } public BigDecimal rupeesToEuro(BigDecimal rupees) { BigDecimal result = rupees.multiply(euroRate); return result.setScale(2, BigDecimal.ROUND_UP); } }
<html> <head> <title>Converter</title> </head> <body bgcolor="white"> <h1>Converter</h1> <hr> <p>Enter an amount to convert:</p> <form method="get" action="index.jsp"> <input type="text" name="amount" size="25"><br> <p> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> <jsp:include page="/ConverterHandler" /> </body>
package webejb; import ejb.Converter; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ConverterHandler extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { @EJB(name = "ejb/Converter") private Converter converter; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String amount = request.getParameter("amount"); if (amount != null && amount.length() > 0) { BigDecimal d = new BigDecimal(amount); BigDecimal rupeeAmount = converter.dollarToRupees(d); out.println("<p>" + amount + " Dollars are " + rupeeAmount + " Rupees.<p>"); BigDecimal euroAmount = converter.rupeesToEuro(rupeeAmount); out.println(amount + " Dollars are " + euroAmount + " Euro."); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2"> <sys:environment> <sys:moduleId> <sys:groupId>default</sys:groupId> <sys:artifactId>WebEJB</sys:artifactId> <sys:version>1.0</sys:version> <sys:type>car</sys:type> </sys:moduleId> <sys:dependencies> <sys:dependency> <sys:groupId>default</sys:groupId> <sys:artifactId>CurrencyConvertEJB</sys:artifactId> <sys:version>1.0</sys:version> <sys:type>car</sys:type> </sys:dependency> </sys:dependencies> </sys:environment> <context-root>/WebEJB</context-root> <nam:ejb-ref xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2"> <nam:ref-name>ejb/Converter</nam:ref-name> <nam:pattern> <nam:groupId>default</nam:groupId> <nam:artifactId>CurrencyConvertEJB</nam:artifactId> <nam:version>1.0</nam:version> <nam:name>ConverterBean</nam:name> </nam:pattern> </nam:ejb-ref> </web-app>
Bookmark this on Delicious Digg this | Privacy Policy - Copyright © 2003-2011, The Apache Software Foundation, Licensed under ASL 2.0. |