Book Image

Advanced Java EE Development with WildFly

By : Deepak Vohra
Book Image

Advanced Java EE Development with WildFly

By: Deepak Vohra

Overview of this book

<p>This book starts with an introduction to EJB 3 and how to set up the environment, including the configuration of a MySQL database for use with WildFly. We will then develop object-relational mapping with Hibernate 4, build and package the application with Maven, and then deploy it in&nbsp;WildFly 8.1, followed by a demonstration of the use of Facelets in a web application.</p> <p>Moving on from that, we will create an Ajax application in the Eclipse IDE, compile and package it using Maven, and run the web application on WildFly 8.1 with a MySQL database. In the final leg of this book, we will discuss support for generating and parsing JSON with WildFly 8.1.</p>
Table of Contents (18 chapters)
Advanced Java EE Development with WildFly
Credits
About the Author
About the Reviewers
www.PacktPub.com
Disclaimer
Preface
Index

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:

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:

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

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.