Creating a JSP client

Next, we will create a JSP client to test the EJB entities. We will look up the session bean using a local JNDI name. Subsequently, we will invoke the testData method of the session bean to test database persistence using these entities. First create a JSP file. Select File | New | Other, and in the New wizard, select Web | JSP File and click on Next, as in the following screenshot:

Creating a JSP client

In the New JSP File wizard, select the jboss-ejb3/web/src/main/webapp folder in the jboss-ejb3-web subproject. Specify catalog.jsp as as File name and click on Next. Then click on Finish:

Creating a JSP client

The catalog.jsp file gets added to the jboss-ejb3-web subproject:

Creating a JSP client

We need to retrieve the CatalogSessionBeanFacade component from the JSP client. WildFly 8 provides the local JNDI (Java Naming and Directory Interface) namespace: Java, and the following JNDI contexts:

JNDI Context

Description

java:comp

This is the namespace that is scoped to the current component, the EJB.

java:module

This namespace is scoped to the current module.

java:app

This namespace is scoped to the current application.

java:global

This namespace is scoped to the application server.

When the jboss-ejb3 application is deployed, the JNDI bindings in the namespaces (discussed in the preceding table) are created as indicated by the server message:

JNDI bindings for session bean named CatalogSessionBeanFacade in deployment unit subdeployment "jboss-ejb3-ejb.jar" of deployment "jboss-ejb3-ear.ear" are as follows:
  java:global/jboss-ejb3-ear/jboss-ejb3-ejb/CatalogSessionBeanFacade!org.jboss.ejb3.model.CatalogSessionBeanFacade
  java:app/jboss-ejb3-ejb/CatalogSessionBeanFacade!org.jboss.ejb3.model.CatalogSessionBeanFacade
  java:module/CatalogSessionBeanFacade!org.jboss.ejb3.model. CatalogSession
BeanFacade
  java:global/jboss-ejb3-ear/jboss-ejb3-ejb/CatalogSessionBeanFacade
  java:app/jboss-ejb3-ejb/CatalogSessionBeanFacade
  java:module/CatalogSessionBeanFacade

Next we will retrieve the session bean façade: CatalogSessionBeanFacade using the standard Java SE JNDI API, which does not require any additional configuration, using the local JNDI lookup in the java:app namespace. For the local JNDI lookup, we need to create an InitialContext object:

Context context = new InitialContext();

Using the local JNDI name lookup in the java:app namespace, retrieve the CatalogSessionBeanFacade component:

CatalogSessionBeanFacade bean = (CatalogSessionBeanFacade) context .lookup("java:app/jboss-ejb3-ejb/CatalogSessionBeanFacade!org.jboss.ejb3.model.CatalogSessionBeanFacade");

Invoke the createTestData method and retrieve the List Catalog entities. Iterate over the Catalog entities and output the catalog ID as the journal name:

bean.createTestData();
List<Catalog> catalogs = beanRemote.getAllCatalogs();
out.println("<br/>" + "List of Catalogs" + "<br/>");
for (Catalog catalog : catalogs) {
  out.println("Catalog Id:");
  out.println("<br/>" + catalog.getId() + "<br/>");
  out.println("Catalog Journal:");
  out.println(catalog.getJournal() + "<br/>");
}

Similarly, obtain the Entity, Section, and Article entities and output the entity property values. The catalog.jsp file is available in the code downloaded for the chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset