Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Step 4: Implementing JSP Clients to Test CMP 2.0 Entity Beans

To test the entity bean application, use JSP clients. For each entity bean, there are two JSPs—one to create the data and the other to search for it. We've written six JSP files: createAddress.jsp and searchAddress.jsp for AddressEJB, createRoster.jsp and searchRoster.jsp for RosterEJB, and createStudent.jsp and searchStudent.jsp for StudentEJB. Because JSP and clients' implementation aren't the focus of this book, we'll look at just one JSP client, SearchStudent.jsp, and briefly explain its logic.

The input form is embedded in the JSP file, as shown next. Embedding the form allows three search options—student ID, last name, or first name.

Search for a Student:
<p>
    <form method="get" action="/cmp3/searchStudent.jsp">
    Search by
    <select name="searchCriteria">
      <option value="studentID" selected>Student ID
      <option value="lastName">Last Name
      <option value="firstName">First Name
    </select>
    <input type="text" name="searchText" size="25">
    <p>
    <input type="submit" value="Search">
    </form>

Depending on the search criteria, SearchStudent.jsp performs a JNDI lookup for a student home object and then calls finder methods, two of which return a collection. It next goes through the array list; then, using the student and roster component interface, it extracts the classes in which the students are enrolled and the students' addresses.

InitialContext ic = new InitialContext();
Object obj = ic.lookup("java:comp/env/ejb/StudentRef");
LocalStudentHome home = (LocalStudentHome) obj;

Collection Students = new ArrayList();
if ("studentID".equals(criteria)) {
    try {
    LocalStudent student = home.findByPrimaryKey(text);
    Students.add(student);
  } catch (ObjectNotFoundException ex) {}
}
else if ("lastName".equals(criteria)) {
    Students = home.findByLastName(text);
}
else if ("firstName".equals(criteria)) {
    Students = home.findByFirstName(text);
}

The following code fragment extracts the LocalStudent interface, invokes business methods, and displays the addresses and classes in an html table as follows:

_____________________.
<% for (int i = 0; i < Students.size(); i++) {
    LocalStudent stud = (LocalStudent)((ArrayList)Students).get(i);
    String sid = (String) stud.getPrimaryKey();
    ArrayList rosterList = stud.getRosterList();
%>
<b> <%=stud.getFirstName()%>  <%=stud.getLastName()%> </b> is registered in
<%=stud.getRosterList().size()%> classes listed below: <p>

<table border=2>
<tr><th>Roster ID</th> <th>Schedule ID</th></tr>
<%
    for (int j=0; j < rosterList.size(); j++)
    {
          LocalRoster rost = (LocalRoster) rosterList.get(j);
%>
<tr>
<td> <%=rost.fetchRosterID()%> </td>
<td> <%=rost.fetchScheduleID()%> </td>
</tr>
<%
    }
%>
</table>
<p>
 and has
<%=stud.getAddressList().size()%> addresses<p>
<table border=3>
<tr><th>Street</th><th>city</th><th>State</th></tr>
<%   ArrayList list = stud.getAddressList();
    for (int k=0; k< list.size(); k++)
    {
      LocalAddress addr = (LocalAddress)list.get(k);
%>
<tr>
<td><%=addr.getStreet()%></td>
<td><%=addr.getCity()%></td>
<td><%=addr.getState()%></td>
</tr>
<%
    }
%>
</table>
<%
  • + Share This
  • 🔖 Save To Your Account