j2ee tutorial

21
Übung SAVES, Sommersemester 2006 Holger Klus Sebastian Herold Technische Universität Kaiserslautern Fachbereich Informatik AG Softwarearchitektur J2EE-Tutorial Developing a J2EE-Application with JBoss

Upload: pariwesh

Post on 13-Nov-2014

38 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: J2EE Tutorial

Übung SAVES, Sommersemester 2006

Holger KlusSebastian Herold

Technische Universität KaiserslauternFachbereich InformatikAG Softwarearchitektur

J2EE-Tutorial

Developing a J2EE-Application with JBoss

Page 2: J2EE Tutorial

Overview

Application Scenario „Drink Account Manager“Current situationGoals of „Drink Account Manager“

J2EE-IntroductionShort OverviewContainer-ConceptEntity BeansSession BeansServlets/JSP‘sPackaging and DeploymentXDoclet

Page 3: J2EE Tutorial

Application Scenario „Drink Account Manager“

Current situationA printed list with available drinks and possible consumers is provided in our kitchenEvery person makes a bar in the corresponding field if he removes a drinkAdditionally a price list is availableEvery 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 kitchenAutomatic generation of bills and the corresponding E-MailBut first: Implementing basic functionality like

- Show/Add/Edit/Delete- Consumers- Drinks- Removals- Prices- Bills

Holger Klus

Sebastian Herold

…Apfelschorle (0,7 Liter)

Cola (0,5 Liter)

Wasser (0,7 Liter)

0,70 €Apfelschorle (0,7 Liter)

0,75 €Cola (0,5 Liter)

0,40 €Wasser (0,7 Liter)

PreisGetränk

Page 4: J2EE Tutorial

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

Page 5: J2EE Tutorial

Relational DB-Schema

pk_id firstName lastName email

tblConsumer

pk_id fk_consumer

tblBilldateOfIssue expirationDate balanced

pk_id fk_bill

tblRemovalfk_consumer fk_drink amount dateOfRemoval

pk_id name

tblDrinkcapacity pk_id fk_drink

tblPriceamount validFrom validUntil

Page 6: J2EE Tutorial

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, andmulti-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

Page 7: J2EE Tutorial

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 wayThey provide services that the components can use either in a programmatic or a declarative wayAllows declarative transaction management (only possible in the EJB-container)

Different types of ContainersApplication ContainerApplet ContainerEJB-Container

- Entity Beans, Session Beans, Message-driven BeansWeb-Container

- Servlets, JSPs

Page 8: J2EE Tutorial

J2EE – Container-Concept

Page 9: J2EE Tutorial

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

Page 10: J2EE Tutorial

J2EE – Bean-Usage

Beans are registered in a JNDI-repositoryLookup by name

Access Beans through interfacesRemote 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)

Page 11: J2EE Tutorial

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 descriptorsNaming convention

- setProperty()- getProperty()

Page 12: J2EE Tutorial

EJB-QL (EJB Query Language)

Defines queries for the finder and select methods of an entity bean with container-managed persistenceThe 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 aSELECT DISTINCT OBJECT(p) FROM Drink d WHERE d.name = ?1 AND d.capacity = ?2

Page 13: J2EE Tutorial

J2EE – Session Beans

Used for realizing superior business logicOften 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 BeansSession Beans represent a classical facade

Entity Bean

Entity BeanEntity Bean

Entity Bean

Session BeanClient

EJB-Container

Page 14: J2EE Tutorial

J2EE – Value Objects / Data Access Objects (DAO)

Value objects/DAOs are simple POJOs (plain old java objects)

Are used to exchange application-specific dataExample: DrinkListEntrySimple POJOs can be generated automatically

Important:Value Objects have to be serializable

Page 15: J2EE Tutorial

J2EE - Servlets

Special Java classes located on the serverAppropriate for the implementation of web-based user interfacesDynamic generation of web content instead of returning static contentThe 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>");…

DisadvantagesHtml mixed with JavaRecompilation required after changes in the source code

Page 16: J2EE Tutorial

J2EE - JSPs

Special html-pages located on the serverThey can be developed like html-pages but can also include Java-code

Naturally appropriate for the implementation of web-based user interfacesNot particular well suited to perform processing logic

JSPs are transformed into Servlets at runtimeNo 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

Importantjsp-tags, names, parameters, … are case-sensitive

Page 17: J2EE Tutorial

J2EE – Packaging and Deployment

PackagingAll 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

Page 18: J2EE Tutorial

J2EE – Deployment and Packaging

DeploymentStep of transferring the J2EE-Application to the application serverOnly 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- …

Page 19: J2EE Tutorial

XDoclet

Open Source code generation engineEnables Attribute-Oriented Programming for Java

Adding meta data to the java sourceXDoclet parses the source files and generates artifacts such as XML descriptors and/or source code from itCurrently XDoclet can only be used as part of the build process utilizing Jakarta AntDetails look at

http://xdoclet.sourceforge.net/xdoclet/index.html

Page 20: J2EE Tutorial

XDoclet – Main Idea

Java-Files

XDoclet-Tags

XDoclet-build.xml

ant

.java web.xml jboss.xml ejb-jar.xml …

Page 21: J2EE Tutorial

Advantages/Disadvantages of J2EE

AdvantagesJ2EE 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

DisadvantagesVery 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