J2EE-Tutorial

Developing a J2EE-Application with JBoss

Übung SAVES, Sommersemester 2006 Holger Klus Sebastian Herold Technische Universität Kaiserslautern Fachbereich Informatik AG Softwarearchitektur

Overview
Application Scenario „Drink Account Manager“
Current situation Goals of „Drink Account Manager“

J2EE-Introduction
Short Overview Container-Concept Entity Beans Session Beans Servlets/JSP‘s Packaging and Deployment XDoclet

Application Scenario „Drink Account Manager“
Current situation
Wasser (0,7 Liter) Cola (0,5 Liter) Apfelschorle (0,7 Liter) …

A printed list with available drinks and possible consumers is provided in our kitchen Every person makes a bar in the corresponding field if he removes a drink Additionally a price list is available Every 4-5 weeks a bill is sent to the consumers by E-Mail

Sebastian Herold Holger Klus

Goals of „Drink Account Manager“

Making bars via Touch-Screen in the kitchen Automatic generation of bills and the corresponding E-Mail But first: Implementing basic functionality like
- Show/Add/Edit/Delete
Consumers Drinks Removals Prices Bills

Getränk
Wasser (0,7 Liter) Cola (0,5 Liter) Apfelschorle (0,7 Liter) …

Preis
0,40 € 0,75 € 0,70 €

Application Scenario „Drink Account Manager“
Implementation of this scenario using two different approaches
Fat-Client-Approach
- Client is a Java application using Hibernate for Object-Relational mapping - All data will be stored in a MySQL-Database

Ultra-Thin-Client-Approach (using J2EE)
- Client accesses the application through a web interface - Web-pages are generated on server-side and will then be sent to the client - The application runs in an application server including
- Business logic and - Persistence functionality - Also: dynamic generation of required web-pages

- Data will also be stored in a MySQL-Database

Relational DB-Schema
tblBill
pk_id fk_consumer dateOfIssue expirationDate balanced

tblConsumer
pk_id firstName lastName email

tblRemoval
pk_id fk_consumer fk_bill fk_drink amount dateOfRemoval

tblDrink
pk_id name capacity

tblPrice
pk_id fk_drink amount validFrom validUntil

J2EE - Short Overview
J2EE ≡ “Java 2 Platform Enterprise Edition”
The newest version is called “Java Platform, Enterprise Edition (Java EE)”

Provides a programming platform for developing and running
distributed, and multi-tier architecture Java applications

J2EE is based on software components executable in an application server
There are specific runtime environments for specific components
- Containers

Allows developers to create an enterprise application that is portable between platforms
„Write once, run anywhere, and reuse everywhere“ Possible, because J2EE is standardized

J2EE – Short Overview
One of the most important concepts in J2EE are Containers

Different types of Containers
Application Container Applet Container EJB-Container Web-Container
- Servlets, JSPs

Provide an environment in which the components can be executed in a controlled and managed way They provide services that the components can use either in a programmatic or a declarative way Allows declarative transaction management (only possible in the EJB-container)

- Entity Beans, Session Beans, Message-driven Beans

J2EE – Container-Concept

J2EE – Enterprise Java Beans (EJB)
Three types of EJBs (all executed in the EJB-Container)
Entity Beans
- Provide an object-oriented view to the underlying persistent data - Container- vs. Bean Managed Persistence - Synchronous access using RMI-IIOP

Session Beans
Modelling business processes Stateless vs. Stateful Session Beans They are conversational and perform specific actions Synchronous access using RMI-IIOP

Message-driven Beans
- Similar to Session Beans but provide asynchronous access using JMS

J2EE – Bean-Usage
Beans are registered in a JNDI-repository
Lookup by name

Access Beans through interfaces
Remote Interface
- Interface to the application-specific services of the bean
- setName() - getName() - getDrinkList()

Home Interface
- Interface for managing bean instances
- create() - findAll() - findByPrimaryKey()

Each type available in local and remote version (since EJB 2.0)

J2EE – Entity Beans
An Entity Bean is a Java class with some additional features/attributes
They can be made persistent in an relational database
- Bean-Managed persistence (BMP)
- The programmer has to implement several callback methods like ejbCreate, ejbRemove, …

- Container-Managed persistence (CMP)

- Only a mapping to the relational DB has to be provided by the programmer, the rest will be managed by the container - Three descriptors involved - mysql-ds.xml (located in jboss-4.0.4RC1\server\default\deploy) - jbosscmp-jdbc.xml - ejb-jar.xml - Mapping of relations between beans is also done in these descriptors

Naming convention - setProperty() - getProperty()

EJB-QL (EJB Query Language)
Defines queries for the finder and select methods of an entity bean with container-managed persistence The scope of an EJB-QL query spans the abstract schemas of related entity beans that are packaged in the same EJB jar-file. They are defined in the deployment descriptor of the entity bean (ejb-jar.xml).
SELECT OBJECT(a) FROM Drink AS a SELECT DISTINCT OBJECT(p) FROM Drink d WHERE d.name = ?1 AND d.capacity = ?2

J2EE – Session Beans
Used for realizing superior business logic Often their methods correspond to use cases and use services of one or many Entity Beans
E.g. Methods which provides appropriate data for the presentation layer

Session Beans encapsulate Entity Beans Session Beans represent a classical facade
Entity Bean Client Session Bean Entity Bean Entity Bean Entity Bean

EJB-Container

J2EE – Value Objects / Data Access Objects (DAO)
Value objects/DAOs are simple POJOs (plain old java objects)
Are used to exchange application-specific data Example: DrinkListEntry Simple POJOs can be generated automatically

Important:
Value Objects have to be serializable

J2EE - Servlets
Special Java classes located on the server Appropriate for the implementation of web-based user interfaces Dynamic generation of web content instead of returning static content The client invokes a servlet using an HTTP request

Servlets can access components running in the EJB container (-> Session Beans) But: html-code is generated using println-statements
PrintWriter out = resp.getWriter(); out.println("<html><head><title>"); …

The web container forwards the request to the servlet. The servlet processes it and generates the content dynamically. The web container then transmits the response back to the web server and finally to the client.

Disadvantages

Html mixed with Java Recompilation required after changes in the source code

J2EE - JSPs
Special html-pages located on the server They can be developed like html-pages but can also include Java-code
Naturally appropriate for the implementation of web-based user interfaces Not particular well suited to perform processing logic

JSPs are transformed into Servlets at runtime
No Recompilation required after changes of the layout

Some important jsp-Tags
<% … %>
- Here you can insert Java code, so called “Scriptlets”

<%@ … %>
- Among others you can insert an import-statement with libraries to be included - Content of this tag is called “Directive”

<jsp:forward>
- Can be used to redirect the request to another jsp-page

Important
jsp-tags, names, parameters, … are case-sensitive

J2EE – Packaging and Deployment
Packaging
All components and deployment descriptors have to be packaged in a specific way .ear
- .war
- *.jsp - WEB-INF - jboss-web.xml - web.xml

- .jar
- META-INF - ejb-jar.xml - jboss.xml - jbosscmp-jdbc.xml

- META-INF
- application.xml

J2EE – Deployment and Packaging
Deployment
Step of transferring the J2EE-Application to the application server Only the .ear-file has to be deployed

Doing all that stuff manually would take a lot of time!
Therefore XDoclet has been developed in order to automate these tasks like
- Generating required interfaces - Generating deployment descriptors - …

XDoclet
Open Source code generation engine Enables Attribute-Oriented Programming for Java
Adding meta data to the java source

XDoclet parses the source files and generates artifacts such as XML descriptors and/or source code from it Currently XDoclet can only be used as part of the build process utilizing Jakarta Ant Details look at
http://xdoclet.sourceforge.net/xdoclet/index.html

XDoclet – Main Idea
XDoclet-Tags

Java-Files

XDoclet-build.xml

ant

.java

web.xml

jboss.xml

ejb-jar.xml

Advantages/Disadvantages of J2EE
Advantages
J2EE provides a complete architecture for developing Separation of technical and application-specific code
- Deployment descriptors - Container Managed Persistence

- Distributed systems including object persistence, session tracking, transaction management, …

Disadvantages

Very complex technology

Many errors occur only at runtime (several steps required until the application is running)
Compilation Packaging Deployment Running the application

- Even simple examples require many interfaces, bean classes, deployment descriptors, …

Sign up to vote on this title
UsefulNot useful