Book Image

Java EE Development with Eclipse

By : Deepak Vohra
Book Image

Java EE Development with Eclipse

By: Deepak Vohra

Overview of this book

<p>Java EE is the industry standard on enterprise computing and Oracle WebLogic Server is the most comprehensive platform for enterprise applications. The book combines Java EE with WebLogic Server in the most commonly used Java IDE, the Eclipse IDE 3.7.<br /><br />"Java EE Development with Eclipse" is the only book on Eclipse IDE for Java EE Developers. The book is a practical guide for using the most commonly used Java EE technologies and frameworks in Eclipse IDE. Sample applications are available in downloadable format and may be customized to meet requirements. Oracle Enterprise Pack for Eclipse 12c, an enhancement to Eclipse IDE, providing additional project facets and an integrated support for Oracle WebLogic Server is used.<br /><br />"Java EE Development with Eclipse" is based on developing applications with some of the commonly used technologies using the project facets in Eclipse 3.7 and its enhancement Oracle Enterprise Pack for Eclipse 12c. <br /><br />The book starts with a discussion on EJB 3.0 database persistence with Oracle database XE and Oracle WebLogic Server. JAXB is discussed in the context of bi-directional mapping between XML and Java. A generic web project is developed for PDF and Excel spread sheet reports. JavaServer Faces, the most commonly used view component in web applications is discussed for developing a data table. Facelets, which was integrated into JSF with 2.0 version is discussed in the context of templating. ADF Faces components are used to develop another user interface (UI) application. Web services are discussed with JAX-WS and JAX-RS technologies. Java EE frameworks Ajax and Spring are also discussed.</p>
Table of Contents (17 chapters)
Java EE Development with Eclipse
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a test client


Next, we shall create a JSP client to test the entity bean. We shall look up the session bean using the global JNDI name, which includes the mapped name for the session bean. Subsequently, we shall invoke the testData method of the session bean to test database persistence using the entity bean. First, create a JSP file. Create a webModule folder for the JSP file by going to File | New | Folder. Specify Folder name as webModule in the EJB3JPA folder and click on Finish. Go to File | New | Other and select JSP File in Web and click on Next. Specify JSP File name as catalog.jsp and click on Next as shown in the following screenshot:

Select the New JSP File (html) template and click on Finish. A JSP client catalog.jsp gets created. Create an InitialContext object.

InitialContext context = new InitialContext();

Look up the remote business interface of the session bean using the global JNDI name:

CatalogSessionBeanFacadeRemote beanRemote = (CatalogSessionBeanFacadeRemote) context.lookup("EJB3-SessionEJB#ejb3.CatalogSessionBeanFacadeRemote");

Invoke the createTestData method and retrieve the List of Catalog entities:

beanRemote.createTestData();
List<Catalog> catalogs=beanRemote.getAllCatalogs();

Iterate over the Catalog entities and output the catalog ID, journal name. Similarly, obtain the Entity, Section, and Article entities and output the entity property values.

The catalog.jsp file

The catalog.jsp file is listed as follows:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="ejb3.*,java.util.*,javax.naming.*"%>
<html>
<head>
<meta http-equiv="Content-Type"
  content="text/html; charset=windows-1252" />
<title>EJB3 Client</title>
</head>
<body>

Create an InitialContext method to look up the session bean facade:

  <%
    InitialContext context = new InitialContext();
    CatalogSessionBeanFacadeRemote beanRemote = (CatalogSessionBeanFacadeRemote) context
        lookup("EJB3-SessionEJB#ejb3.CatalogSessionBeanFacadeRemote"); 

Invoke the session bean method to create test data. Subsequently, retrieve the entity beans and output the values of entities' properties:

    beanRemote.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/>");
    }

    out.println("<br/>" + "List of Editions" + "<br/>");
    List<Edition> editions = beanRemote.getAllEditions();
    for (Edition edition : editions) {
      out.println("Edition Id:");
      out.println(edition.getId() + "<br/>");
      out.println("Edition Date:");
      out.println(edition.getEdition() + "<br/>");
    }
    out.println("<br/>" + "List of Sections" + "<br/>");
    List<Section> sections = beanRemote.getAllSections();
    for (Section section : sections) {
      out.println("Section Id:");
      out.println(section.getId() + "<br/>");
      out.println("Section Name:");
      out.println(section.getSectionname() + "<br/>");

    }

    out.println("<br/>" + "List of Articles" + "<br/>");
    List<Article> articles = beanRemote.getAllArticles();
    for (Article article : articles) {
      out.println("Article Id:");
      out.println(article.getId() + "<br/>");
      out.println("Article Title:");
      out.println(article.getTitle() + "<br/>");

    }
    out.println("Delete some Data" + "<br/>");

Invoke the session bean method to delete some data. Subsequently, retrieve the entities and output the properties of different entities:

    beanRemote.deleteSomeData();

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

    out.println("<br/>" + "List of Editions" + "<br/>");
    editions = beanRemote.getAllEditions();
    for (Edition edition : editions) {
      out.println("Edition Id:");
      out.println(edition.getId() + "<br/>");
      out.println("Edition Date:");
      out.println(edition.getEdition() + "<br/>");

    }
    out.println("<br/>" + "List of Sections" + "<br/>");
    sections = beanRemote.getAllSections();
    for (Section section : sections) {
      out.println("Section Id:");
      out.println(section.getId() + "<br/>");
      out.println("Section Name:");
      out.println(section.getSectionname() + "<br/>");

    }
    out.println("<br/>" + "List of Articles" + "<br/>");
    articles = beanRemote.getAllArticles();
    for (Article article : articles) {
      out.println("Article Id:");
      out.println(article.getId() + "<br/>");
      out.println("Article Title:");
      out.println(article.getTitle() + "<br/>");
    }
  %>
</body>
</html>