Overview
This example creates a session bean that contains business logic to calculate the total amount of money accrued for a savings scheme. This session bean is accessed by an application client and web client (Part i. below). You are then required to add a mobile client (Part ii. below).
Part i. Create J2EE Application
The following steps are followed:
Business-tier components run on the J2EE server.
1. Create session bean to represent business logic - Calculator
Client-tier components run on the client machine.
2. Create an application client to access the Calculator
3. Create JSP web client to access the Calculator
Note: Use simple configuration on local host i.e
Part ii. Mobile Client
Build a mobile client for the calculator.
In your system the server should return a result and the number of calculation requests a user has done in a session. Both the result and the number of calculations should be shown on the client.
Hint: Build the basic system, then add the number of calculations functionality.
For part(ii) document the following:
i. Code of the programs.
ii. Output from a complete execution of the system. (screen short)
iii. Brief explanation of system behaviour with respect to all application components.
Details
Part i. Create Enterprise Application(with Application Client) : CalculatorApp
0.File>New Project
1. Select Catorgories:Enterprise, Projects:Enterprise Application
2. Next>
3. Add Name:CalculatorApp
4. Select Creat Application Client Module
5. Change Project location to your directories
6. To Set Server select Manage...
7. Select Add Server .....
8. Next window not shown, but in Select box select: Sun Java System Application Server.
9. Next>
Browse to Platform location
10.Next>
11. Add Password
12. Select Finish until Project created
Create Session Bean to hold Business Logic
13. Right click ejb Project > new > Session Bean
14. Add : name= Calculator; Package=ejb;
15. Select Remote and Local Interfaces > Finish
16. Add calculate() method
Right click in code > EJB Methods
Wizard Updates interfaces automatically ( beta version required restart to get this
option to show!)
Add name
Use Add... Button to add methods parameters
17. Calculate Session Bean code:
package ejb;
import javax.ejb.Stateless;
@Stateless
public class CalculatorBean implements CalculatorRemote, CalculatorLocal {
/** Creates a new instance of CalculatorBean */
public CalculatorBean() {
}
public double calculate(int startAge, int endAge, double growthRate,
double savings) {
double tmp =
Math.pow(1. + growthRate / 12., 12. * (endAge - startAge) + 1);
return savings * 12. * (tmp - 1) / growthRate;
}
}
Create Application Client
18. Navigate to the main.java class in the client project
(see screen shot below)
19. Import bean reference:
right click in code area > Enterprise Resources> Call enterprise bean
Select Referenced Interface: Remote
>o.k
18. Client Class main.java
20. Add access code:
Use Alt-Shift-F to generate any necessary imports.
public static void main(String[] args) {
System.out.println("Data to Calculator Bean ");
System.out.println("int start=25, int end=65,
double growthrate=0.01, double saving=£350 ");
try {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
// Access bean
double d = cal.calculate(25,65,0.01,350);
System.out.println("The balance at the End age is " +
nf.format(d));
} catch (Exception e) {
System.out.println("error in Bean Access");;}
}
Run Application Client
21. Set client type from Application properties:
right click on Application project
select:
properties/run/client module URI
select CalculatorApp-app-clientwar
Uncheck Display Browser on Run
Deploy and Run Application
22. Right click on Application > Run Project
Note deployment to server and output.
Create Web Client
23. Navigate to the index.jsp class in the web project
(see screen shot below)
24. Replace index code with:
Note code to obtain session bean reference
"https://www.w3.org/TR/html4/loose.dtd">
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="ejb.CalculatorLocal, javax.naming.*, java.text.*"%>
<%!
private CalculatorLocal cal = null;
public void jspInit () {
try {
System.out.println("JSP init");
InitialContext ctx = new InitialContext();
cal = (CalculatorLocal)ctx.lookup(
"java:comp/env/local_ref");
} catch (Exception e) {
System.out.println("EJB REFERENCE PROBLEM");
e.printStackTrace ();
}
}
%>
<%
String result;
int start = 25;
int end = 65;
double growthrate = 0.08;
double saving = 300.0;
try {
start = Integer.parseInt(request.getParameter ("start"));
end = Integer.parseInt(request.getParameter ("end"));
growthrate = Double.parseDouble(request.getParameter ("growthrate"));
saving = Double.parseDouble(request.getParameter ("saving"));
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
result = nf.format(cal.calculate(start, end, growthrate, saving));
} catch (Exception e) {
result = "Please press - Calculate";}
%>
JSP Page
Investment calculator2
The result from the last calculation: The balance at the End age is £
<%=result%>
23. Index.jsp
25. Set client to web client
right click on Application project
select:
properties/run/client module URI
select CalculatorApp-war.war
check Display Browser on Run
Set bean reference in Descriptor
26. Input references in web.xml as below to set .
Double click on web.xml
Select References tab
Expand EJB References
Select Add
Deploy and Run Application
27. Right click on Application > Run Project
Note deployment to server and web browser output.
26. Descriptor References
Attachment:- Logbook1_Exercise_D_new-1.rar