Você está na página 1de 21

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 Cola (0,5 Apfelschorle …


ƒ A printed list with available drinks (0,7 Liter) Liter) (0,7 Liter)
and possible consumers is provided
in our kitchen
Sebastian
ƒ Every person makes a bar in the Herold
corresponding field if he removes a
drink Holger Klus

ƒ Additionally a price list is available


ƒ Every 4-5 weeks a bill is sent to the …
consumers by E-Mail

ƒ Goals of „Drink Account Manager“


ƒ Making bars via Touch-Screen in the Getränk Preis
kitchen Wasser (0,7 Liter) 0,40 €
ƒ Automatic generation of bills and the
corresponding E-Mail Cola (0,5 Liter) 0,75 €
ƒ But first: Implementing basic
functionality like Apfelschorle (0,7 Liter) 0,70 €
- Show/Add/Edit/Delete
- Consumers …
- Drinks
- Removals
- Prices
- Bills
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 tblPrice
pk_id name capacity 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
ƒ 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)
ƒ Different types of Containers
ƒ Application Container
ƒ Applet Container
ƒ EJB-Container
- Entity Beans, Session Beans, Message-driven Beans
ƒ Web-Container
- Servlets, JSPs
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
ƒ 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.
ƒ 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>");
ƒ …
ƒ 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
- Distributed systems including object persistence, session tracking,
transaction management, …
ƒ Separation of technical and application-specific code
- Deployment descriptors
- Container Managed Persistence
ƒ Disadvantages
ƒ Very complex technology
- Even simple examples require many interfaces, bean classes, deployment
descriptors, …
ƒ Many errors occur only at runtime (several steps required until the
application is running)
- Compilation
- Packaging
- Deployment
- Running the application

Você também pode gostar