Creating and testing a test client
In this section, we create a JSP client to test the entity bean using a wrapper
session bean.
Creating a client
First, we need to create a JSP. Select the EJB3ViewController project and select
File>New. In the New Gallery window, select Categories | Web Tier | JSP and
Items | JSP. Click on OK.

In the Create JSP window, specify a FileName (EJB3Client) and click on OK.

As we shall be invoking the entity bean, which is in the model project, from the
JSP, which is in the ViewController project, we need to add a dependency in the
ViewController project on the Model project. Select Tools | Project Properties and
select Dependencies. Click on the Edit Dependencies button.

In the Edit Dependencies window, select EJB3Model | Build Output and click on
OK, as shown:

The EJB3Model project gets added to the Dependencies. Click on OK, as
shown next:

In the JSP client, we look up the session bean and invoke the test() method on it,
which returns a String. First, we create an InitialContext:
InitialContext context = new InitialContext();
Two methods are available to look up a session bean using the remote
business interface.
- Look up the session bean remote interface using the mapped name. The
global JNDiname for a session bean remote business interface is derived
from the remote business interface name. The format of the global JNDI
name is mappedName#qualified_name_of_businessInterface. - Specify the business interface JNDiname in the weblogic-ejb-jar.xml
deployment descriptor. The global JNDiname is specified as follows:
<weblogic-enterprise-bean>
<ejb-name>CatalogTestSessionEJBBean</ejb-name>
<stateless-session-descriptor>
<business-interface-jndi-name-map>
<business-remote>CatalogTestSessionEJB
</business-remote>
<jndi-name>EJB3-SessionEJB</jndi-name>
</business-interface-jndi-name-map>
</stateless-session-descriptor>
</weblogic-enterprise-bean>
We shall use the first method. Create a remote business interface instance using
lookup with the mapped name:
CatalogTestSessionEJB beanRemote = (CatalogTestSessionEJB) context.
lookup(“EJB3-SessionEJB#model.CatalogTestSessionEJB”);
Invoke the test() method of the session bean:
String catalog=beanRemote.test();
Output the string returned by the test method:
<%=catalog %>
The EJB3Client is listed next:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<%@ page import=”model.*, javax.naming.*” %>
<%@ page
contentType=”text/html;charset=windows-1252″%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html;
charset=windows-1252″ />
<title>EJB3Client</title>
</head>
<body><% InitialContext context = new InitialContext();
CatalogTestSessionEJB beanRemote = (CatalogTestSessionEJB)
context.lookup(“EJB3-SessionEJB#model.CatalogTestSessionEJB”);
String catalog=beanRemote.test(); %><%=catalog %></body>
</html>
Testing the client
To run the test client, right-click on, EJB3Client.jsp and select Run.

The output from the test client lists a catalog entry, all the titles, and all the entity
instances after removing a catalog entry.

Summary
In this section, we created an EJB 3.0 entity bean in JDeveloper 11g from an Oracle
database table. The Catalog entity bean is automatically created a database table
CATALOG; the database table columns are mapped to entity bean properties. We
created a wrapper session bean for the entity bean, including a remote business
interface. We added a test method to the session bean for creating and persisting
entity instances, querying entity instances, and removing an entity instance. We
created a JSP test client to test the entity bean. We look up the session bean remote
interface using the mapped name for the session bean and invoke the test method on
the remote interface instance. In the next chapter, we shall discuss EJB 3.0 database
persistence with Oracle Enterprise Pack for Eclipse and WebLogic server.
EJB 3.0 Articles
- JPA in NetBeans 6.1
- EJB 3.0 and WebServices
- Introduction to Java Persistence API(JPA)
- EJB 3.0 Timer Services – An Overview






September 5, 2010
EJB 3.0