In this lab, you will write simple stateless session EJB. You can use the sample in the course as a guide.
In this part, you will configure the JEE server that you will use for the labs.
The JEE application server you will use in labs is the Apache TomEE server.
Ask your instructor for the following information:
TomEE Installation directory: ________________________________________
If your computer has the standard lab setup on a computer running a version of Windows before Windows Vista, this directory will be:
TomEE Installation Directory: c:\java\apache-tomee-plus-1.7.1
If your computer is running Windows Vista, Windows 7 or later, the directory will be:
TomEE Installation directory: c:\Users\username\java\apache-tomee-plus-1.7.1
In Eclipse, switch perspectives by choosing from the menu Window - Open Perspective - Other - Java EE (default).
At the bottom of the Eclipse window, click the Servers tab. Then right-click on the blank window (or click on the ...Click this link to create a new server link to start the Create-Server wizard. Complete the server creation:
At the top of the server list, expand the Apache entry, then highlight the Tomcat 7.0 Server entry. (Note that the TomEE server you will use is a superset of Tomcat.)
For the Server name, edit the name so it's Tomcat v7.0 (TomEE) Server at localhost.
Press Next.
On the next page, for the Tomcat installation directory, click the Browse button and navigate to {Lab Installation Directory}/java/apache-tomee-plus-1.7.1.
If your computer is running Windows XP and has the standard lab setup:
c:\java\apache-tomee-plus-1.7.1
If your computer has Windows Vista or later:
c:\Users\username\java\apache-tomee-plus-1.7.1
Press OK followed by Finish.
You should see the server in the Servers pane. Right-click on the server and note that you can start, stop, and add/remove projects. You don't need to do any of these right now.
You need to create a project that will hold your application. Follow these steps:
From the Eclipse menu, choose File - New - Dynamic Web Project.
On the first wizard page, for the Project name, enter Lab03Web.
Note that the target server is your Tom EE server.
Press Next.
On the next wizard page, note the defaults, then press Next.
On the next page, put a checkmark in the Generate web.xml deployment descriptor box, then press Finish.
You should see the new project in the Project Explorer view.
Right-click on the project, then choose New - Other - EJB - Session Bean (EJB 3.x) and press Next to start the wizard.
On the first wizard page, for the Java package, enter ship.
For the Class name, enter ShippingBean.
Uncheck the No-interface View box, then put a checkmark next to the Remote box, then, for the remote interface name, change the name to ship.ShippingRemote and press Next.
On the next wizard page, uncheck the boxes for Inherited abstract methods and Constructors from superclass, then press Finish.
Eclipse creates the business interface and the implementation class and opens the class into the Java editor.
Look in the Project Explorer and note that you can see the source files under the Java Resources/src/ship folder.
Double-click ShippingRemote to open it into the Java editor. Note how the wizard inserted the @Remote annotation to configure this as a remote interface.
In the interface, define the calcDomestic method that returns a hypothetical shipping-charge calculation:
public double calcDomestic(String zip);
Press Ctrl+S to save and compile.
Open ShippingBean.java (the class). Note how the wizard inserted the @Stateless annotation to configure the EJB as a stateless session bean. In the class, write a simple implementation method that returns a hard-coded shipping charge:
public double calcDomestic(String zip)
{
return 3.55;
}
Press Ctrl+S to save and compile.
In this part, you will write a Web application that acts as a client to the EJB.
Create a simple servlet that will invoke the EJB:
In the Project Explorer, in the Lab03Web project, right-click on Java Resources: src and choose New - Servlet to start the wizard.
On the first wizard page, for the Java package enter servlets.
For the Class name, enter ShippingServlet, then press Next.
On the next page, note the URL Mapping, which assigns a URL to the servlet, then press Next.
On the next wizard page, uncheck the doPost checbox, then press Finish. Eclipse opens the servlet class into the editor.
Complete the servlet:
Within the class, but outside of any method, write the following:
@EJB
private ShippingRemote shipping;
This defines a field to hold a reference to the EJB. The @EJB annotation instructs the server to inject the EJB's reference into the field, thus initializing it.
Find the doGet method. This method will be invoked when the user submits a Web page you will write in a moment. In doGet, write the following code to read a parameter from the invoking Web page and then call the EJB:
double amt = shipping.calcDomestic(
request.getParameter("zipCode"));
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Shipping rate: " + amt);
From the Eclipse menu, choose Source - Organize Imports to cause Eclipse to determine which Java import statements are needed by your code.
Press Ctrl+S to save and compile - you should have no compile errors.
Next, create a simple Web page with an HTML form that references your servlet:
In the Lab03Web project, right-click on the WebContent folder and choose New - HTML to start the wizard.
On the first wizard page, for the File Name, enter index.html then press Next.
On the next wizard page, for the Template, select New HTML File (5), then press Finish.
Modify the generated HTML so that the body contains an HTML form that lets the user enter a zip code and invokes your servlet. Your resulting HTML might look something like:
<html>
<head>
<title>Calculate Shipping</title>
</head>
<body>
<form action="ShippingServlet">
<p>
Enter zip: <input type="text" name="zipCode">
</p>
<p>
<input type="submit" value="Submit">
</p>
</form>
</body>
</html>
Be sure to press Ctrl+S to save the HTML file.
Now you can test your program. In the Project Explorer, in the Lab03Web/WebContent folder, right-click on the index.html and choose Run As - Run on Server and press Finish.
Wait for Eclipse to display your Web page, then enter any zip code and press Submit. You should see the your hard-coded shipping calculation.
Use File - Close All to close all open editor windows.
In the Servers view, right-click on the server entry and choose Add and Remove, then remove the project from the server. This undeploys your application.