Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Practical Database Programming With Java

.pdf
Скачиваний:
778
Добавлен:
10.06.2015
Размер:
31.58 Mб
Скачать

5.3 Exploring NetBeans IDE 6.8 219

enterprise bean module deployment descriptor, for example, declares transaction attributes and security authorizations for an enterprise bean. A Java EE module can be deployed as a standalone module.

The four types of Java EE modules are listed as follows:

1.EJB modules, which contain class files for enterprise beans and an EJB deployment descriptor. EJB modules are packaged as JAR files with a .jar extension.

2.Web modules, which contain Servlet class files, Web files, supporting class files, GIF and HTML files, and a Web application deployment descriptor. Web modules are packaged as JAR files with a .war (Web ARchive) extension.

3.Application client modules, which contain class files and an application client deployment descriptor. Application client modules are packaged as JAR files with a .jar extension.

4.Resource adapter modules, which contain all Java interfaces, classes, native libraries, and other documentation, along with the resource adapter deployment descriptor. Together, these implement the Connector architecture for a particular EIS. Resource adapter modules are packaged as JAR files with a .rar (resource adapter archive) extension.

5.3.5.1.5 Java EE 6 APIs An EJB component, or enterprise bean, is a body of code having fields and methods to implement modules of business logic. You can think of an enterprise bean as a building block that can be used alone or with other enterprise beans to execute business logic on the Java EE server.

There are two kinds of enterprise beans: session beans and message-driven beans.

1. A session bean represents a transient conversation with a client. When the client finishes executing, the session bean and its data are gone.

2.A message-driven bean combines features of a session bean and a message listener, allowing a business component to receive messages asynchronously. Commonly, these are Java Message Service (JMS) messages.

Java EE includes several API specifications, such as JDBC, RMI, email, JMS, Web services, XML, etc, and defines how to coordinate them. Java EE also features some specifications unique to Java EE for components. These include Enterprise JavaBeans (EJB), Connectors, Servlets, portlets (following the Java Portlet specification), JavaServer Pages, and several web service technologies. This allows developers to create enterprise applications that are portable and scalable, and that integrate with legacy technologies. A Java EE application server can handle transactions, security, scalability, concurrency and management of the components that are deployed to it in order to enable developers to concentrate more on the business logic of the components rather than on infrastructure and integration tasks.

The Java EE APIs includes several technologies that extend the functionality of the base Java SE APIs. Table 5.9 shows most components included in Enterprise Edition 6 API Specification.

5.3.5.1.6 Java EE 6 APIs Included in the Java SE 6 Several APIs that are required by the Java EE 6 platform are included in the Java SE 6 platform and are thus available to Java EE applications. Table 5.10 lists these APIs.

220 Chapter 5 Introduction to NetBeans IDE

Table 5.9. Most components defined in Enterprise Edition 6 API Specification

Component Name

Description

 

 

javax.ejb.*

The EJB specification defines a set of lightweight APIs that an

 

object container (the EJB container) will support in order to

 

provide Java Transaction API (JTA) remote procedure calls

 

using Remote Method Invocation (RMI), concurrency control,

 

dependency injection, and access control for business objects.

 

This package contains the EJB classes and interfaces that define

 

the contracts between the enterprise bean and its clients, and

 

between the enterprise bean and the EJB container

javax.enterprise.context.*

These packages define the context (scope) annotations and

 

interfaces for the Contexts and Dependency Injection (CDI) API

javax.enterprise.inject.*

These packages define the injection annotations for the Contexts

 

and Dependency Injection (CDI) API

javax.jms.*

This package defines the Java Message Service (JMS) API. The

 

JMS API provides a common way for Java programs to create,

 

send, receive, and read an enterprise messaging system’s

 

messages

javax.faces.*

This package defines the root of the JavaServer Faces (JSF) API.

 

JSF is a technology for constructing user interfaces out of

 

components

javax.faces.component.*

This package defines the component part of the JavaServer Faces

 

(JSF) API. Since JSF is primarily component oriented, this is one

 

of the core packages. The package overview contains a UML

 

diagram of the component hierarchy

javax.persistence

This package contains the classes and interfaces that define the

 

contracts between a persistence provider and the managed

 

classes and the clients of the Java Persistence API (JPA)

javax.xml.stream

This package contains readers and writers for XML streams

javax.resource.*

This package defines the Java EE Connector Architecture API.

 

Java EE Connector Architecture (JCA) is a Java-based

 

technology solution for connecting application servers and

 

enterprise information systems (EIS) as part of enterprise

 

application integration (EAI) solutions

 

 

5.3.5.1.7 Java EE 6 Application Servers To build a Java EE application, developers can use some certified Application Servers provided by Sun. Here are some popular Java EE6 related application servers. The servers are categorized based on the following groups:

1.Java EE 6 certified servers

Sun GlassFish Enterprise Server v3 based on the open source GlassFish application server

JEUS 7, an application server from TmaxSoft. According to their website, “JEUS 7 is scheduled to be released at the end of 2010.”

2.In development for full Java EE 6

JBoss Application Server 6

 

5.3 Exploring NetBeans IDE 6.8 221

Table 5.10. Java Enterprise Edition 6 APIs included in Java Standard Edition 6 API

 

 

API Name

Description

 

 

Java Database

The Java Database Connectivity (JDBC) API lets you invoke

Connectivity API

SQL commands from Java programming language methods.

 

You use the JDBC API in an enterprise bean when you have a

 

session bean access the database. You can also use the JDBC

 

API from a Servlet or a JSP page to access the database

 

directly without going through an enterprise bean.

 

The JDBC API has two parts: an application-level interface used

 

by the application components to access a database, and a

 

service provider interface to attach a JDBC driver to the Java

 

EE platform

Java Naming and

The Java Naming and Directory Interface (JNDI) provides

Directory Interface

naming and directory functionality, enabling applications to

 

access multiple naming and directory services, including existing

 

naming and directory services such as LDAP, NDS, DNS, and

 

NIS. It provides applications with methods for performing

 

standard directory operations, such as associating attributes

 

with objects and searching for objects using their attributes.

 

Using JNDI, a Java EE application can store and retrieve any

 

type of named Java object, allowing Java EE applications to

 

coexist with many legacy applications and systems

JavaBeans Activation

The JavaBeans Activation Framework (JAF) is used by the

Framework

JavaMail API. JAF provides standard services to determine the

 

type of an arbitrary piece of data, encapsulate access to it,

 

discover the operations available on it, and create the

 

appropriate JavaBeans component to perform those operations

Java API for XML

The Java API for XML Processing (JAXP), part of the Java SE

Processing

platform, supports the processing of XML documents using

 

Document Object Model (DOM), Simple API for XML (SAX),

 

and Extensible Style sheet Language Transformations (XSLT).

 

JAXP enables applications to parse and transform XML

 

documents independent of a particular XML processing

 

implementation

Java Architecture for

The Java Architecture for XML Binding (JAXB) provides a

XML Binding (JAXB)

convenient way to bind an XML schema to a representation in

 

Java language programs. JAXB can be used independently or in

 

combination with JAX-WS, where it provides a standard data

 

binding for web service messages. All Java EE application client

 

containers, web containers, and EJB containers support the

 

JAXB API.

SOAP with

The SOAP with Attachments API for Java (SAAJ) is a low-level

Attachments API for

API on which JAX-WS and JAXR depend. SAAJ enables the

Java

production and consumption of messages that conform to the

 

SOAP 1.1 and 1.2 specifications and SOAP with Attachments

 

note. Most developers do not use the SAAJ API, instead using

 

the higher-level JAX-WS API

(Continued)

222 Chapter 5 Introduction to NetBeans IDE

Table 5.10. (Continued)

API Name

Description

 

 

Java API for XML Web

The JAX-WS specification provides support for web services that

Services (JAX-WS)

use the JAXB API for binding XML data to Java objects. The

 

JAX-WS specification defines client APIs for accessing web

 

services, as well as techniques for implementing web service

 

endpoints. The Implementing Enterprise Web Services

 

specification describes the deployment of JAX-WS-based

 

services and clients. The EJB and Java Servlet specifications

 

also describe aspects of such deployment. It must be possible to

 

deploy JAX-WS-based applications using any of these

 

deployment models

Java Authentication

The Java Authentication and Authorization Service (JAAS)

and Authorization

provides a way for a Java EE application to authenticate and

Service (JAAS)

authorize a specific user or group of users to run it.

 

JAAS is a Java programming language version of the standard

 

Pluggable Authentication Module (PAM) framework, which

 

extends the Java Platform security architecture to support

 

user-based authorization

 

 

3.In development for Java EE 6 Web Profile

• Caucho Resin 4.0. Discussion

Next, let’s build a Java EE 6 project to illustrate how to use some important components we have discussed, such as Java EJB, JPA, and JFS, to develop a three-tier application to access a sample database via application server to perform data actions.

5.3.5.2 Install and Configure Java EE 6 Software and Tools

To build and develop a Java EE 6 Web application, the following software and tools are needed:

Java EE 6 Software Development Kit (SDK)

Apache Ant

Since most Java EE 6 Web applications are three-tier applications, which mean:

Web browser works as the top tier

Enterprise server works as the mid-tier

Database server works as the third tier

Sun GlassFish Enterprise Server v3 is targeted as the build and runtime environment for the Java EE 6 Web applications. To build, deploy, and run a Java EE 6 project, you need a copy of the Enterprise Server. To obtain the Enterprise Server, you must install the Java EE 6 Software Development Kit (SDK), which you can download from

5.3 Exploring NetBeans IDE 6.8 223

http://java.sun.com/javaee/downloads/. Make sure you download the Java EE 6 SDK, not the Java EE 6 Web Profile SDK.

Let’s first take care of downloading the Java EE 6 SDK.

5.3.5.2.1 Install Java EE 6 SDK Software

Go to the site http://java.sun.com/javaee/

downloads/ to open the GlassFish and

Java EE 6 page. Select Windows and

English from the Platform and Language combo box, and click on the Download button under the Java EE 6 SDK column to begin this downloading process.

On the opened Download dialog, click on the Continue to Download button to skip this registration step. Click on the Save button to temporarily save this software to the Temp folder under the root (C:/) driver in your computer.

When the download is done, you can click on the Run button to install it to your computer. Follow the installation instructions to complete this process.

During the installation of the SDK, pay special attention to the following steps:

1.Configure the Enterprise Server administration Username as the default setting (admin). You can select and enter any password as you like (reback is used in our application).

2.Accept the default port values for the Admin Port (4848) and the HTTP Port (8080), unless the Port has been occupied. In that case, change the Port number to enable system to use other Port. In our application, change the HTTP Port to 8082 since the Port 8080 has been used by some other devices.

3.Allow the installer to download and configure the Update Tool. If you access the Internet through a firewall, provide the proxy host and port.

Click on the Install button to begin this installation process, as shown in Figure 5.55.

Figure 5.55. The installation process of Java EE 6 SDK.

224 Chapter 5 Introduction to NetBeans IDE

In the Registration step, click on the Skip Registration radio button if you do not want to perform this registration. Click on the Next button to continue.

When the installation is complete, a Summary page is displayed to provide a detailed installation and configuration report for this installation. Click on the Exit button to complete this installation if everything looks fine. Refer to Appendix I for more details on this installation.

5.3.5.2.2 Add Enterprise Server as a Server in the NetBeans IDE To run the Java EE 6 Web applications in the NetBeans IDE, you must register your Enterprise Server installation as a NetBeans Server Instance. Follow these instructions to register the

Enterprise Server in the NetBeans IDE.

1.

Launch the NetBeans IDE 6.8.

2.

Select the Tools

> Servers menu item to open the Servers dialog.

3.

Click on the Add

Server button.

4.

In the opened Add Server Instance dialog, select the GlassFish v3, as shown

 

in Figure 5.56, and click on the Next button.

5.

Under Server

Location, enter the location of your Enterprise Server installation,

 

which is C:\glassfishv3, and click on the Next button.

6.

Select the Register Local Domain radio button

7.

Click on the Finish button.

Your finished installed GlassFish v3 server window should match one that is shown in Figure 5.57. Click on the Close button to complete this adding process.

Now let’s create a new Java EE 6 Web application project.

5.3.5.3 Create a Java EE 6 Web Application Project

In this section, we will build a Java EE 6 Web application, combined with a brief introduction to some features introduced as part of Java EE 6 specification we discussed in the

Figure 5.56. Add the GlassFish v3 Enterprise Server into the NetBeans IDE.

5.3 Exploring NetBeans IDE 6.8 225

Figure 5.57. The finished adding glassfish v3 server into the NetBeans IDE window.

previous sections. To illustrate the new features, we will demonstrate how to create a simple Java EE 6 Web application that contains an EJB 3.1 stateless session bean facade for an entity class. We will use wizards in the NetBeans IDE to generate the entity class and the session bean. The code generated by the wizard uses queries that are defined in the Criteria API that is part of JPA 2.0 and contained in the Java EE 6 specification. We will then create a named managed bean that accesses the session facade and a presentation layer that uses the Facelets view framework as specified in JSF 2.0.

This project is based on technologies in the Java EE 6 specification, such as JavaServer Faces 2.0 (JSF), Enterprise Java Beans 3.1 (Session Bean and Message-Driven Bean), and JPA, with the help of NetBeans IDE 6.8 as the tool. We will creates a Java EE 6 Web application called JavaEEDBManufacturer that performs only the retrieving function on the manufacturer records in the Manufacturer table provided by the NetBeans sample database served by the Glassfish built-in database server, JavaDB.

The objective of this project is to demonstrate the ease of using several Java EE 6 technologies like JSF2.0, EJB3, and JPA with the help of NetBeans IDE, and putting them together to create an enterprise-ready Web-based application.

Figure 5.58 shows a structure diagram of this Web application project.

Perform the following operations to create a new Java EE 6 Web application project named JavaEEWebDBApp:

1.Launch the NetBeans IDE 6.8.

2.Choose File > New Project (Ctrl-Shift-N) from the main menu.

3.Select Enterprise Application from the Java EE category and click on the Next button.

4.Type JavaEEDBManufacturer for the project name and set the desired project location.

226 Chapter 5 Introduction to NetBeans IDE

Java EE Application

Client Tier

Client Machine

 

Java Server Faces

Web Tier

JSP Pages

Java EE Server

 

 

 

Session Bean

 

 

Message Driven Bean

 

 

 

 

 

 

 

 

 

 

 

Message listener

Queue

Enterprise Beans

Business Tier

Java Persistence API

Data Access

 

 

Database

EIS Tier

Database Server

Figure 5.58. The structure diagram of the Java EE 6 Web application project.

Figure 5.59. The finished New Enterprise Application window.

5. Deselect the Use Dedicated Folder option, if selected. Click on the Next button. For this application, there is little reason to copy project libraries to a dedicated folder because we will not need to share libraries with other users or projects.

6.Set the server to GlassFish v3, and set the Java EE Version to Java EE 6. Keep all other default settings and click on the Finish button.

Your finished New Enterprise Application window should match one that is shown in Figure 5.59.

5.3 Exploring NetBeans IDE 6.8 227

Figure 5.60. Created three projects by NetBeans IDE.

NetBeans will create three projects, namely JavaEEDBManufacturer

(Enterprise Application project), JavaEEDBManufacturer-ejb (EJB project), and JavaEEDBManufacturer-war (Web project), as shown in Figure 5.60.

Next, let’s create our entity classes to map our sample database and tables, since the Session Beans are responsible for manipulating the data, and they will be created in the EJB project (refer to Fig. 5.58).

5.3.5.4 Creating the Entity Classes from the Database

Perform the following operations to create our entity classes for our sample database:

1.In the Projects window, right click on the JavaEEDBManufacturer-ejb project and select the New > Entity Classes from Database item from the pop-up menu.

2.

Check the Data Source Radio button, click on the dropdown arrow, and select the New

 

Data Source from the corresponding dropdown list.

3.

On the opened Create Data Source dialog, enter jdbc/sample into the JNDI

 

Name field and click on the dropdown arrow on the Database Connection combo

 

box, and select the default JavaDB sample database connection URL jdbc:derby://

 

localhost:1527/sample [app on APP]. Click on the OK button to close this

 

dialog box.

4.

Under the Available Tables list box, select MANUFACTURER and click on Add button

 

so that it appears in the Selected Tables list box. Your New Entity Classes

 

from Database window should match one that is shown in Figure 5.61. Click on the

 

Next button to continue.

 

 

Note: You do not need to create a new Data Source; instead, you can directly select the jdbc/sample from the existing data source if this default database has been installed and set up in your NetBeans IDE.

5.Click on the Create Persistence Unit button and select jdbc/sample as the

Data Source. Leave the rest as default as shown in Figure 5.62, and click on the Create button to continue.

228 Chapter 5 Introduction to NetBeans IDE

Figure 5.61. The New Entity Classes from Database window.

Figure 5.62. The Create Persistence Unit dialog.

6.Provide a package name, com.javaeedbmanufacturer.entity, in the Package

field and click on the Next button.

7.Change the Collection Type to java.util.List and click on the Finish button to complete this entity class creation process.

You can find that one entity class, Manufacturer.java, has been created under the

Source Packages, com.javaeedbmanufacturer.entity, in the Projects window, which is shown in Figure 5.63.

Next, let’s create the Java Beans to perform communication functions between the JSF pages and JPA to make the data actions against our sample database.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]