Click on the images to view them full size!
Step 13 - Developing a Web Client to use the EJB
Now, for the users to access the bean, we need to develop a client application, which will be a simple web application. We will create a web project as part of the EmployeeEAR project. Right Click on EmployeeEAR project and in the pop-up, click New->Project. In the New Project Wizard select Web->Dynamic Web Project. Type in the name as EmployeeWeb and click Finish
We must add the EmployeeEJB project to the Java build path of our web project. To do this, right click EmployeeWeb project and select Properties->Java Build Path. Select EmployeeEJB and click OK.
Now, we will create a Java bean to transport the employee details. Right click on the EmployeeWeb project and select New->Class. Give the package name as 'employee.web' and the class name as 'EmployeeData'. click Finish.
Add the fields as below and generate the getters and setters for these fields.
id int
firstName String
lastName String
Here is the complete listing of the bean class:
package employee.web;
public class EmployeeData {
private int id = 0;
private String firstName = null;
private String lastName = null;
public String getFirstName() {
return firstName;
}
public int getId() {
return id;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String string) {
firstName = string;
}
public void setId(int i) {
id = i;
}
public void setLastName(String string) {
lastName = string;
}
}
Now, we will create a servlet, which talks to the beans. To do this, right click EmployeeWeb Project and select New->Servlet. Type in the package name as 'employee.web.servlets' and the Servlet name as 'EmployeeServlet'. Click Finish.
Our code to access the bean comes inside the doPost() method, as we will be posting the request from a client form. Here is the complete listing of the EmployeeServlet class. Copy and paste the code from here to your newly created servlet class.
package employee.web.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.naming.*;
import java.util.*;
import javax.rmi.*;
import employee.web.*;
import employee.ejb.*;
public class EmployeeServlet extends HttpServlet {
String firstName = null;
String lastName = null ;
int id = 0;
EmployeeData employeeData = new EmployeeData();
String JNDIName = "ejb/employee/ejb/EmployeeSessionHome";
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
EmployeeSessionHome employeeSessionHome = null;
EmployeeSession employeeSession = null;
InitialContext initContext = null;
id = Integer.parseInt(request.getParameter("id"));
firstName = request.getParameter("firstName") == null ? "" : request.getParameter("firstName") ;
lastName = request.getParameter("lastName") == null ? "" : request.getParameter("lastName") ;
// Get the initial context
try
{
initContext = new InitialContext();
Object obj = initContext.lookup(JNDIName);
employeeSessionHome = (EmployeeSessionHome) PortableRemoteObject.narrow(obj,EmployeeSessionHome.class);
}
catch (javax.naming.NamingException e)
{
e.printStackTrace();
}
// Create a new EmployeeSession
try
{
employeeSession = employeeSessionHome.create();
// talk to the CMP Entity bean
String firsNamefromEJB = employeeSession.getFirstName(id);
String lastNamefromEJB = employeeSession.getLastName(id);
// set the values returned into the JavaBean
employeeData.setId(id);
employeeData.setFirstName(firsNamefromEJB);
employeeData.setLastName(lastNamefromEJB);
}
catch (java.rmi.RemoteException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
// Place the bean in the session to pass to JSP
HttpSession session = request.getSession(true);
session.setAttribute("employeedetails",employeeData);
// Now, forward the response to the Results.jsp page
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("results.jsp");
dispatcher.forward(request, response);
}
}
Moving on to the last part of our project, let's create the HTML form page to submit our request to the servlet and the JSP page to display the response.
Right Click EmployeeWeb Project in Poject Navigator and select New->HTML/XHTML File. Type in the file name as 'index.html' and select markup language to be HTML.
Copy and paste the below code to your HTML file.
<HTML>
<HEAD>
<TITLE>Sample EJB Client Application</TITLE>
</HEAD>
<BODY>
<form name=test method="POST" action="servlet/employee.web.servlets.EmployeeServlet">
Employee ID <INPUT name="id" size="3" type="text">
<input type="Submit" value="View Emplyee Details">
</form>
</BODY>
</HTML>
Similarly, create a JSP page and name it results.jsp and copy paste the below code.
<HTML>
<HEAD>
<TITLE>Results Page</TITLE>
</HEAD>
<BODY>
<jsp:useBean id="employeedetails" class="employee.web.EmployeeData" scope="session"/>
<FONT color="#0000ff" face="Arial" size="+2"><b> Employee Details</FONT>
<HR>
<BR>
ID Number: <jsp:getProperty name="employeedetails" property="id"/> <BR>
<BR>
First Name: <jsp:getProperty name="employeedetails" property="firstName"/>
<br>
<BR>
Last Name: <jsp:getProperty name="employeedetails" property="lastName"/>
</BODY>
</HTML>
Save all the changes now. Select EmployeeWeb and click on 'Rebuild Project' in the Project menu to build the Project.
Now, start TestServer. Right-click index.html in the Project Navigator and click 'Run on Server'. Select TestServer in the server list(if not selected by default) and click Finish.
This will open up index.html page in Universal Test Client Browser. Enter a valid Employee ID and click 'View Details' button.
Now, here is the list of processes involved in fetching you the Employee's Details.
1. The HTML form posts the data to the EmployeeServlet
2. EmployeeServlet looks up the JNDI registry to locate the EmployeeSessionHome.
3. EmployeeServlet invokes the create method on EmployeeSessionHome which return the EmployeeSession remote Object.
4. EmployeeServlet then invokes the getter methods on the EmployeeSession remote object.
5. These methods, in turn calls the getEmployeeEntity() method of EmployeeSessionBean which looks up the JNDI registry to find the EmployeeEntityHome, and invokes the findByPrimaryKey() method on it.
6. The Container looks for the EJB-RDB mapping to find out the DataSource and the Table/Schema kj defenitions.
7. The container now locates the entity in the backend tables and fetches it.Also, it
takes out one of the Entity Beans from the pool and loads it with the data fetched from the backend.
8. The findByPrimaryKey() method of EmployeeEntityHome returns a reference to the fetched EmployeeEntityBean's remote object.
9. The getter methods on the remote object gets called which returns the field values
10. The EmployeeServlet instantiates the EmployeeData bean and sets it's field values with the fetched values, and adds this bean to the session, and forwards the response to results.jsp
11. The results.jsp page gets loaded and displays the fields of the bean contained in the session.
If all of these steps work well, we will see the result as below:
Upto now, we have been using the WSAD built in Test enviornment to test our project. In the next steps,we will see how to package the whole projec to an enterprise archive (.ear) and deploy it in a Standalone Application Server.
Go to : Part 1Part 2Part 3Part 4Part 5Part 6
No comments:
Post a Comment