PART II Building Three-Tier Client–Server Applications 555 Chapter 8 Developing Java Web Applications to Access Databases 557
5.3.5 Build a Java Enterprise Edition Project
Java Platform, Enterprise Edition or Java EE is a widely used platform for server pro- gramming in the Java programming language. The Java EE differs from the Java Standard Edition Platform (Java SE) in that it adds libraries that provide functionality to deploy fault - tolerant, distributed, multi - tier Java software, based largely on modular components running on an application server.
Although Java EE is closely related to Java Web applications, it is so important to Java application developments, and therefore we prefer to provide a detailed discussion in this section to give readers a preintroduction about this critical component.
c05.indd 214
c05.indd 214 7/20/2011 11:11:19 AM7/20/2011 11:11:19 AM
www.traintelco.com
5.3.5.1 Overview of Java Enterprise Edition 6
The aim of the Java EE platform is to provide developers with a powerful set of APIs while reducing development time, reducing application complexity, and improving appli- cation performance. The Java EE platform uses a simplifi ed programming model. XML deployment descriptors are optional. Instead, a developer can simply enter the informa- tion as an annotation directly into a Java source fi le, and the Java EE server will confi gure the component at deployment and runtime. These annotations are generally used to embed in a program data that would otherwise be furnished in a deployment descriptor.
With annotations, the specifi cation information is put directly in your code next to the program element that it affects.
Java EE is a widely used platform containing a set of coordinated technologies that signifi cantly reduce the cost and complexity of developing, deploying, and managing multitier, server - centric applications. Java EE builds upon the Java SE platform and pro- vides a set of APIs for developing and running portable, robust, scalable, reliable, and secure server - side applications.
Some of the fundamental components of Java EE include:
• Enterprise JavaBeans (EJB): a managed, server - side component architecture used to encap- sulate the business logic of an application. EJB technology enables rapid and simplifi ed development of distributed, transactional, secure, and portable applications based on Java technology.
• JPA: a framework that allows developers to manage data using object - relational mapping ( ORM ) in applications built on the Java Platform.
5.3.5.1.1 Java EE Application Model Java EE is designed to support applications that implement enterprise services for customers, employees, suppliers, partners, and others who make demands on or contributions to the enterprise. Such applications are inherently complex, potentially accessing data from a variety of sources and distributing applications to a variety of clients.
The Java EE application model defi nes an architecture for implementing services as multitier applications that deliver the scalability, accessibility, and manageability needed by enterprise - level applications. This model partitions the work needed to implement a multitier service into two parts: the business and presentation logic to be implemented by the developer, and the standard system services provided by the Java EE platform.
The developer can rely on the platform to provide solutions for the hard systems - level problems of developing a multitier service.
Java EE is defi ned by its specifi cation. As with other Java Community Process speci- fi cations, providers must meet certain conformance requirements in order to declare their products as Java EE compliant, which is shown in Figure 5.52 .
5.3.5.1.2 Distributed Multitiered Applications The Java EE platform uses a distributed multitiered application model for enterprise applications. Application logic is divided into components according to function, and the various application components that make up a Java EE application are installed on different machines depending on the tier in the multitiered Java EE environment to which the application component belongs.
Figure 5.53 shows a multitiered Java EE application divided into the tiers described in the following list. The Java EE application parts shown in Figure 5.53 are presented in Java EE Components.
• Client - tier components run on the client machine.
• Web - tier components run on the Java EE server.
• Business - tier components run on the Java EE server.
• Enterprise information system ( EIS ) - tier software runs on the EIS server.
The enterprise information system tier handles EIS software and includes enterprise infrastructure systems, such as enterprise resource planning ( ERP ), mainframe transac- tion processing, database systems, and other legacy information systems. For example, Java EE application components might need access to enterprise information systems for database connectivity.
Although a Java EE application can consist of the three or four tiers shown in Figure 5.53 , Java EE multitiered applications are generally considered to be three - tiered applica- tions because they are distributed over three locations: client machines, the Java EE
Figure 5.52. Java EE Specifi cations.
Java EE Specifications
JDBC RMI e-mail JMS Web Services XML
EJB
Connectors Servlets Portlets
JSP
Figure 5.53. A multitiered Java EE application.
Java EE Application
Database
Client Tier
Java EE Server
Database Server Application Server
JSP Pages Web Tier
Client Machine
Enterprise Beans Business Tier
EIS Tier
c05.indd 216
c05.indd 216 7/20/2011 11:11:19 AM7/20/2011 11:11:19 AM
www.traintelco.com
server machine, and the database or legacy machines at the back end. Three - tiered appli- cations that run in this way extend the standard two - tiered client and server model by placing a multithreaded application server between the client application and back - end storage.
5.3.5.1.3 Java EE Components Java EE applications are made up of components.
A Java EE component is a self - contained functional software unit that is assembled into a Java EE application with its related classes and fi les and that communicates with other components.
The Java EE specifi cation defi nes the following Java EE components:
• Application clients and Applets are components that run on the client machine.
• Java Servlet, JavaServer Faces, and JavaServer Pages ( JSP ) technology components are web components that run on the server.
• Enterprise JavaBeans (EJB) components are business components that run on the server.
Java EE components are written in the Java programming language and are compiled in the same way as any program in the language. The difference between Java EE com- ponents and standard Java classes is that Java EE components are assembled into a Java EE application, are verifi ed to be well formed and in compliance with the Java EE speci- fi cation, and are deployed to production, where they are run and managed by the Java EE server.
A Web client consists of two parts:
1. Dynamic web pages containing various types of markup language (HTML, XML, and so on), which are generated by Web components running in the Web tier.
2. A Web browser, which renders the pages received from the server.
A Web client is sometimes called a thin client . Thin clients usually do not query databases, execute complex business rules, or connect to legacy applications. When you use a thin client, such heavyweight operations are off - loaded to enterprise beans execut- ing on the Java EE server, where they can leverage the security, speed, services, and reli- ability of Java EE server - side technologies.
An Application client runs on a client machine and provides a way for users to handle tasks that require a richer user interface than can be provided by a markup language. It typically has a GUI created from the Swing or the Abstract Window Toolkit (AWT) API, but a command - line interface is certainly possible.
Application clients directly access enterprise beans running in the business tier.
However, if application requirements warrant it, an application client can open an HTTP connection to establish communication with a Servlet running in the Web tier.
Application clients written in languages other than Java can interact with Java EE servers, enabling the Java EE platform to interoperate with legacy systems, clients, and non - Java languages.
Java EE Web components are either Servlets or web pages created using JavaServer Faces technology and/or JSP technology (JSP pages). Servlets are Java programming language classes that dynamically process requests and construct responses. JSP pages are text - based documents that execute as Servlets but allow a more natural approach to
creating static content. JavaServer Faces technology builds on Servlets and JSP technol- ogy, and provides a user interface component framework for Web applications.
Static HTML pages and applets are bundled with Web components during applica- tion assembly but are not considered Web components by the Java EE specifi cation.
Server - side utility classes can also be bundled with Web components, and, like HTML pages, are not considered Web components.
A Java EE application is packaged into one or more standard units for deployment to any Java EE platform - compliant system. Each unit contains:
• A functional component or components (such as an enterprise bean, webpage, servlet, or applet)
• An optional deployment descriptor that describes its content
Once a Java EE unit has been produced, it is ready to be deployed. Deployment typically involves using a platform ’ s deployment tool to specify location - specifi c informa- tion, such as a list of local users that can access it and the name of the local database.
Once deployed on a local platform, the application is ready to run.
5.3.5.1.4 Java EE Packaging Applications A Java EE application is delivered in either a Java Archive ( JAR ) fi le, a Web Archive ( WAR ) fi le, or an Enterprise Archive ( EAR ) fi le. A WAR or EAR fi le is a standard JAR (.jar) fi le with a .war or .ear extension.
Using JAR, WAR, and EAR fi les and modules makes it possible to assemble a number of different Java EE applications using some of the same components. No extra coding is needed; it is only a matter of assembling (or packaging) various Java EE modules into Java EE JAR, WAR, or EAR fi les.
An EAR fi le that is shown in Figure 5.54 contains Java EE modules, and, optionally, deployment descriptors. A deployment descriptor is an XML document with an .xml extension that describes the deployment settings of an application, a module, or a component. Because deployment descriptor information is declarative, it can be changed without the need to modify the source code. At runtime, the Java EE server reads the deployment descriptor and acts upon the application, module, or component accordingly.
A Java EE module consists of one or more Java EE components for the same con- tainer type, and, optionally, one component deployment descriptor of that type. An
Figure 5.54. EAR fi le structure.
Assembly Root
META-INF Web Module Application Client Module
Resource Adapter Module
EJB Module
application.xml
c05.indd 218
c05.indd 218 7/20/2011 11:11:19 AM7/20/2011 11:11:19 AM
www.traintelco.com
enterprise bean module deployment descriptor, for example, declares transaction attri- butes 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 fi les for enterprise beans and an EJB deployment descrip- tor. EJB modules are packaged as JAR fi les with a .jar extension.
2. Web modules, which contain Servlet class fi les, Web fi les, supporting class fi les, GIF and HTML fi les, and a Web application deployment descriptor. Web modules are packaged as JAR fi les with a .war (Web ARchive) extension.
3. Application client modules, which contain class fi les and an application client deployment descriptor. Application client modules are packaged as JAR fi les 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 fi les with a .rar (resource adapter archive) extension.
5.3.5.1.5 Java EE 6 API s An EJB component, or enterprise bean, is a body of code having fi elds 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 fi n- ishes 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 specifi cations, such as JDBC, RMI, email, JMS, Web services, XML, etc, and defi nes how to coordinate them. Java EE also features some specifi cations unique to Java EE for components. These include Enterprise JavaBeans (EJB), Connectors, Servlets, portlets (following the Java Portlet specifi cation), 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 Specifi cation.
5.3.5.1.6 Java EE 6 API s 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.
Table 5.9. Most components defi ned in Enterprise Edition 6 API Specifi cation
Component Name Description
javax.ejb. * The EJB specifi cation defi nes 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 defi ne the contracts between the enterprise bean and its clients, and between the enterprise bean and the EJB container
javax.enterprise.context. * These packages defi ne the context (scope) annotations and interfaces for the Contexts and Dependency Injection ( CDI ) API javax.enterprise.inject. * These packages defi ne the injection annotations for the Contexts
and Dependency Injection (CDI) API
javax.jms. * This package defi nes 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 defi nes the root of the JavaServer Faces (JSF) API.
JSF is a technology for constructing user interfaces out of components
javax.faces.component. * This package defi nes 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 defi ne 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 defi nes 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 certifi ed 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 certifi ed 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
c05.indd 220
c05.indd 220 7/20/2011 11:11:19 AM7/20/2011 11:11:19 AM
www.traintelco.com
Table 5.10. Java Enterprise Edition 6 APIs included in Java Standard Edition 6 API
API Name Description
Java Database Connectivity API
The Java Database Connectivity ( JDBC ) API lets you invoke 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 Directory Interface
The Java Naming and Directory Interface ( JNDI ) provides 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 Framework
The JavaBeans Activation Framework ( JAF ) is used by the 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
Processing
The Java API for XML Processing ( JAXP ), part of the Java SE 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 XML Binding ( JAXB )
The Java Architecture for XML Binding (JAXB) provides a 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
Attachments API for Java
The SOAP with Attachments API for Java ( SAAJ ) is a low - level API on which JAX - WS and JAXR depend. SAAJ enables the production and consumption of messages that conform to the SOAP 1.1 and 1.2 specifi cations and SOAP with Attachments note. Most developers do not use the SAAJ API, instead using the higher - level JAX - WS API
(Continued)
API Name Description Java API for XML Web
Services ( JAX - WS )
The JAX - WS specifi cation provides support for web services that use the JAXB API for binding XML data to Java objects. The JAX - WS specifi cation defi nes client APIs for accessing web services, as well as techniques for implementing web service endpoints. The Implementing Enterprise Web Services specifi cation describes the deployment of JAX - WS - based services and clients. The EJB and Java Servlet specifi cations also describe aspects of such deployment. It must be possible to deploy JAX - WS - based applications using any of these
deployment models Java Authentication
and Authorization Service ( JAAS )
The Java Authentication and Authorization Service ( JAAS ) provides a way for a Java EE application to authenticate and authorize a specifi c 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
Table 5.10. (Continued)
3. In development for Java EE 6 Web Profi le • Caucho Resin 4.0. Discussion
Next, let ’ s build a Java EE 6 project to illustrate how to use some important compo- nents we have discussed, such as Java EJB, JPA, and JFS, to develop a three - tier applica- tion to access a sample database via application server to perform data actions.
5.3.5.2 Install and Confi gure 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
c05.indd 222
c05.indd 222 7/20/2011 11:11:19 AM7/20/2011 11:11:19 AM
www.traintelco.com