Home

 

Creating a test Web application

To test the EJB 3.0 session bean and entity model, we create a small Web application with one servlet:

Select File Æ New Project Æ Web Æ Dynamic Web Project:

Enter RAD75EJBTestWeb as name.
Select 2.5 for Dynamic Web Module version.
Add the project to the RAD75EJBEAR enterprise application.
Click Finish and close the help that opens.

Right-click the RAD75EJBTestWeb project and Properties. In the Properties dialog, Java EE Module Dependencies page, select the RAD75EJB.jar module, and click OK.

Right-click the RAD75EJBTestWeb project and select New Æ Servlet.

Enter itso.test.servlet as package name and BankTest as class name. Click Next twice, and select to generate doPost and doGet methods. Click Finish.

After the class definition, add an injector for the business interface:

@javax.ejb.EJB EJBBankService bank;

The injection of the business interface into the servlet resolves to the automatic binding of the session EJB.

In the doGet method, enter the code:

doPost(request, response);

Complete the doPost method with the code of Example | 4-17, which is available in 7672code\ejb\source\BankTest.txt. This servlet executes the methods of the session bean, after getting a reference to the business interface.

Example 14-17 Servlet to test the EJB 3.0 module (abbreviated)

protected void doPost(HttpServletRequest request, 
		HttpServletResponse response) throws ServletException, IOException {
	try {
		PrintWriter out = response.getWriter();
		String partialName = request.getParameter("partialName");
		out.println("<html><body><h2>Customer Listing</h2>");
		if (partialName == null) partialName = "%";
		else partialName = "%" + partialName + "%";
		
		out.println("<p>Customers by partial Name: " + partialName + "<br>");
		Customer[] customers = bank.getCustomers(partialName);
		
		for (Customer cust : customers) {
			out.println("<br>" + cust);
		}
		
		Customer cust1 = bank.getCustomer("222-22-2222");
		out.println("<p>" + cust1);
	
		Account[] accts = bank.getAccounts(cust1.getSsn());
		out.println("<br>Customer: " + cust1.getSsn() + " has " 
											+ accts.length + " accounts");
		Account acct = bank.getAccount("002-222002");
		out.println("<p>" + acct);
		
		out.println("<p>Transactions of account: " + acct.getId());
		Transaction[] trans = bank.getTransactions("002-222002");
		out.println("<p><table border=1><tr><th>Type</th><th>Time</th>...");
		for (Transaction t : trans) {
			out.println("<tr><td>" + t.getTransType() + "</td><td>" + ...);
		}
		out.println("</table>");
		
		String newssn = "xxx-xx-xxxx";
		bank.deleteCustomer(newssn);      // for rerun
		out.println("<p>Add a customer:  " + newssn);
		Customer custnew = new Customer();
		custnew.setSsn(newssn);
		custnew.setTitle("Mrs");
		custnew.setFirstName("Julia");
		custnew.setLastName("Roberts");
		bank.addCustomer(custnew);
		Customer cust2 = bank.getCustomer(newssn);
		out.println("<br>" + cust2);
		
		out.println("<p>Open two accounts for customer: " + newssn);
		String id1 = bank.openAccount(newssn);
		String id2 = bank.openAccount(newssn);
		out.println("<br>New accounts: " + id1 + " " + id2);
		Account[] acctnew = bank.getAccounts(newssn);
		out.println("<br>Customer: " +newssn + " has " +acctnew.length ...);
		Account acct1 = bank.getAccount(id1);
		out.println("<br>" + acct1);
		
		out.println("<p>Deposit and withdraw from account: " + id1);
		bank.deposit(id1, new java.math.BigDecimal("777.77"));
		bank.withdraw(id1, new java.math.BigDecimal("111.11"));
		acct1 = bank.getAccount(id1);
		out.println("<br>Account: " +id1+ " balance " + acct1.getBalance());
		
		trans = bank.getTransactions(id1);
		out.println("<p><table border=1><tr><th>Type</th><th>Time</th>...");
		for (Transaction t : trans) {
			out.println("<tr><td>" + t.getTransType() + ...");
		}
		out.println("</table>");
		
		out.println("<p>Close the account: " + id1);
		bank.closeAccount(newssn, id1);
		
		out.println("<p>Update the customer: " + newssn);
		bank.updateCustomer(newssn, "Mr", "Julius", "Roberto");
		cust2 = bank.getCustomer(newssn);
		out.println("<br>" + cust2);
		out.println("<p>Delete the customer: " + newssn);
		bank.deleteCustomer(newssn);
		
		out.println("<p>Retrieve non existing customer: ");
		Customer cust3 = bank.getCustomer("zzz-zz-zzzz");
		out.println("<br>customer: " + cust3);
		
		out.println("<p>End</body></html>");
	} catch (Exception e) {
		System.out.println("Exception: " + e.getMessage());
		e.printStackTrace();
	}
}

ibm.com/redbooks