Here we'll see a simple JAVA EE application named converter. The purpose of converter is to calculate currency conversions between dollar, taka and ringit. The converter application consists of an enterprise bean, which performs the calculations, and a web client.
Platforms:
1. GlassFish Server
2. NetBeans
Overview:
1. Create Enterprise Application: Converter
2. Create the enterprise bean: ConverterBean.
3. Create the web client.
4. Deploy converter.ear into the server.
5. Using a browser, run the web client.
1. Create Enterprise Application: Converter
2. Create the enterprise bean: ConverterBean.
3. Create the web client.
4. Deploy converter.ear into the server.
5. Using a browser, run the web client.
1. Create Enterprise Application: Converter.
Go to File->New Project (Ctrl + Shift + N) and follow following steps.
After clicking Finish, three project nodes will be created: Converter, Converter-ejb, Converter-war.
2. Now i'll create a class named ConverterBean under Converter-ejb node. This is an Enterprise Bean by @Stateless annotation. This class implements 2 business methods dollarToTaka and takaToRingit. Because the enterprise bean class doesn't implement a business interface, the enterprise bean exposes a local, no-interface view. The public methods in the enterprise bean class are available to clients that obtain a reference to ConverterBean. The source code is as follows:
package converter.ejb;
import java.math.BigDecimal;
import javax.ejb.Stateless;
@Stateless
public class ConverterBean {
private BigDecimal takaRate = new BigDecimal("80");
private BigDecimal ringitrRate = new BigDecimal("0.054");
public BigDecimal dollarToTaka(BigDecimal dollars)
{
BigDecimal result = dollars.multiply(takaRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
public BigDecimal takaToRingit(BigDecimal taka)
{
BigDecimal result = taka.multiply(ringitrRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
}
import java.math.BigDecimal;
import javax.ejb.Stateless;
@Stateless
public class ConverterBean {
private BigDecimal takaRate = new BigDecimal("80");
private BigDecimal ringitrRate = new BigDecimal("0.054");
public BigDecimal dollarToTaka(BigDecimal dollars)
{
BigDecimal result = dollars.multiply(takaRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
public BigDecimal takaToRingit(BigDecimal taka)
{
BigDecimal result = taka.multiply(ringitrRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
}
3. Now i'll create a servlet named ConverterServlet and modify the index.jsp under Converter-war node. A JAVA servlet is a web component that responds to HTTP request.
The main part of ConverterServlet class is that it uses dependency injection to obtain a reference to ConverterBean. The javax.ejb.EJB annotation is added to the declaration of the private member variable converterBean, which is of type ConverterBean. ConverterBean exposes a local, no-interface view, so the enterprise bean implementation class is the variable type.
The source code of ConverterServlet is as follows:
package com.servlets;
import converter.ejb.ConverterBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import converter.ejb.ConverterBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="ConverterServlet", urlPatterns="/convert")
public class ConverterServlet extends HttpServlet {
@EJB
ConverterBean converterBean;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.write("Invalid Access");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher dispatcher = req.getRequestDispatcher("index.jsp");
String dollarText = req.getParameter("dollar");
if(dollarText.isEmpty() || dollarText==null)
{
req.setAttribute("error", "Please Give Dollar Amount");
dispatcher.forward(req, resp);
}
try
{
BigDecimal dollar = new BigDecimal(dollarText);
BigDecimal taka = converterBean.dollarToTaka(dollar);
BigDecimal ringit = converterBean.takaToRingit(taka);
req.setAttribute("dollar", dollar);
req.setAttribute("taka", taka);
req.setAttribute("ringit", ringit);
//System.out.println(dollar + "~" + taka + "~" + ringit);
}
catch(Exception e)
{
req.setAttribute("error", "Number Required");
//e.printStackTrace();
//System.out.println(e.getMessage());
}
dispatcher.forward(req, resp);
}
}
public class ConverterServlet extends HttpServlet {
@EJB
ConverterBean converterBean;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.write("Invalid Access");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher dispatcher = req.getRequestDispatcher("index.jsp");
String dollarText = req.getParameter("dollar");
if(dollarText.isEmpty() || dollarText==null)
{
req.setAttribute("error", "Please Give Dollar Amount");
dispatcher.forward(req, resp);
}
try
{
BigDecimal dollar = new BigDecimal(dollarText);
BigDecimal taka = converterBean.dollarToTaka(dollar);
BigDecimal ringit = converterBean.takaToRingit(taka);
req.setAttribute("dollar", dollar);
req.setAttribute("taka", taka);
req.setAttribute("ringit", ringit);
//System.out.println(dollar + "~" + taka + "~" + ringit);
}
catch(Exception e)
{
req.setAttribute("error", "Number Required");
//e.printStackTrace();
//System.out.println(e.getMessage());
}
dispatcher.forward(req, resp);
}
}
The index.jsp file is as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Convert Currency</title>
</head>
<body>
<h1>Convert Currency!</h1>
<form action="./convert" method="post">
<table>
<tr>
<td>Dollar: </td>
<td><input type="text" name="dollar" value="${dollar}"/>${error}</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Convert"/>
</td>
</tr>
<tr>
<td>
Taka:
</td>
<td>
${taka}
</td>
</tr>
<tr>
<td>
Ringit:
</td>
<td>
${ringit}
</td>
</tr>
</table>
</form>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Convert Currency</title>
</head>
<body>
<h1>Convert Currency!</h1>
<form action="./convert" method="post">
<table>
<tr>
<td>Dollar: </td>
<td><input type="text" name="dollar" value="${dollar}"/>${error}</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Convert"/>
</td>
</tr>
<tr>
<td>
Taka:
</td>
<td>
${taka}
</td>
</tr>
<tr>
<td>
Ringit:
</td>
<td>
${ringit}
</td>
</tr>
</table>
</form>
</body>
</html>
4. Now clean and build the Converter to create Converter.ear and deploy it into GlassFish Server.
5. Run using default port i.e http://localhost:8080/Converter-war/ And test the application.
That's all about this example.
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. DevsData
ReplyDelete