Java 2 Enterprise Edition

rangaleclickΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

61 εμφανίσεις

Java 2 Enterprise Edition

A Brief Overview

What is an Enterprise?


“The term
enterprise
refers to an organization of
individuals or entities, presumably working together
to achieve some common goals.”


Organizations come in all shapes and sizes, large and
small, for
-
profit and nonprofit, governmental and
nongovernmental.


The entire organization


All divisions, departments, etc.


Typically a large organization

Enterprise Software
-

Characteristics


Enterprise applications solve business problems.


This usually involves the safe storage, retrieval, and
manipulation of business data: customer invoices, mortgage
applications, flight bookings, and so on.


They might have multiple user interfaces:



a web interface for consumers and a


graphical user interface (GUI) application running on computers in the
branch offices


Enterprise applications must deal with


communication between remote systems


coordinate data in multiple stores,


and ensure the system always follows the rules laid down by
the business.

Enterprise Software

Enterprise Software:
Characteristics


Following are a few characteristics of enterprise
applications:


Reliable, i.e., bug free and fully tested


Ever
growing enterprise, increased number of users


Scalability of application


Bringing the same level of services to a diversely located enterprise


Distribution


Connecting previously separate systems, legacy systems


Integration


Communicate
with other
applications


Portable


Transaction support (including distributed transactions)


Security


Separate
server
-
side code from client
-
side
presentation


Easily Maintainable & Extensible


Reusable

Java Platforms


Platform introduced June, 1999


There
are four platforms of the Java programming
language:

1.
Java
Platform, Standard Edition (Java SE)

2.
Java
Platform, Enterprise Edition (Java EE)

3.
Java
Platform
, Micro
Edition (Java ME)

4.
Java
FX

Java Platforms

1.
Java
SE (Pre
-
requisite)


Basic Java Programming


Java SE's
API provides the core functionality of the Java programming
language


Defines basic
types
and
objects
for networking
, security, database access,
graphical user interface (
GUI)
and
XML.

2.
Java EE (Core)


Java
EE platform is built on top of the Java SE
platform


Provides an API
and runtime environment for developing and running large
-
scale,
multi
-
tiered,
scalable, reliable
, and secure network applications.

3.
Java ME


Provides
an API and a small
-
footprint virtual machine for running
Java
applications
on small devices, like mobile
phones, PDAs, and etc.

4.
Java
FX


Java FX technology is a platform for creating rich internet applications written in
Java FX Script.


Java ME and Java FX can be used as clients
of Java
EE platform
services.

What is Java EE?


Java EE is a suite of specifications for APIs, a
distributed computing architecture, and definitions for
packaging of distributable components for
deployment.


It’s a collection of standardized components,
containers, and services for creating and deploying
distributed applications within a well
-
defined
distributed computing architecture.


Sun’s Java web site says, “ Java Platform, Enterprise
Edition
6
(Java EE
6)
defines the standard for
developing component
-
based multitier enterprise
applications.”

What is Java EE?


There is a great deal of infrastructure required to
write enterprise
-
class applications.


Java EE defines a set of containers, connectors, and
components that provide this infrastructure


Java EE is based on a well
-
known, published
specifications.


Applications written for Java EE will run on any
number of Java EE
-
compliant implementations.


The reference implementation supplied with the Java
EE Software Development Kit from Sun (Java EE
SDK) provides a working model that we’ll use, since
it’s the implementation that Sun has built from the
specification and is freely available.

Java EE vs J2SE


Java EE
isn’t a replacement
for the Java 2 Standard Edition
(J2SE).


J2SE provides the essential language framework on which Java
EE builds. It is the core on which Java EE is based.


Java EE consists of several layers, and J2SE is right at the base
of that pyramid for each component of Java EE.


You have already learned how to build user interfaces with the
Swing or Abstract Window Toolkit (AWT) components. You’ll
still be using those to build the user interfaces for your Java EE
applications, as well as HTML
-
based user interfaces.


Since J2SE is at the core of Java EE, everything that you’ve
learned so far remains useful and relevant.


You’ll use familiar J2SE components and APIs in conjunction
with the Java EE components and APIs to build that part of
your applications.


J2EE Architecture


J2EE multi
-
tiered
applications are generally
considered to be three
-
tiered applications because
they are distributed over
three different locations


client machines


the J2EE server machine


the database machine at the
back end

J2EE Architecture


Three
-
tiered applications
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

J2EE Tiered
Architecture


You
are all familiar with concept of tiered architecture


In software, a tier is primarily an abstraction and its main purpose
is to help us understand the architecture associated with a
specific application by breaking down the software into distinct,
logical tiers.


There are three fundamental tiers for a
software
application


The presentation
layer (Client Tier)


program that makes requests to the middle tier.


The business logic
layer (Business Tier,
i.e
, middle tier)


Functions handle client requests and process application data, storing it in
a permanent data store in the data tier.


The
data access
layer (Data Tier)


Contains the actual data to be processed


Java
EE
concentrates
on the middle tier to make enterprise
application management
easier, more robust, and
secure
.

J2EE Containers


The application server maintains control and provides services
through an interface or framework known as a
container


There are five defined container types in the J2EE
specification


Three of these are server
-
side containers:


The server itself, which provides the J2EE runtime environment
and the other two containers


An EJB container to manage EJB components


A Web container to manage servlets and JSP pages


The other two container types are client
-
side:


An application container for stand
-
alone GUIs, console


An applet container, meaning a browser, usually with the Java Plug
-
in

J2EE Components


As said earlier, J2EE applications are made up of
components


A
J2EE component

is a self
-
contained functional software
unit that is assembled into a J2EE application with its
related classes and files and that communicates with
other
components


Client components run on the client machine, which
correlate to the client containers


Web components
-
servlets and JSP pages


EJB Components

17

J2EE Server and Containers





Application

Client

Container





EJB Container





Web Container

Application

Client

Web Browser

Enterprise

Bean

Enterprise

Bean

Servlet

JSP Page

J2EE Server

Database

18

Container Services


Security


Transaction Management


Naming and directory


Remote connectivity


Lifecycle management


Persistance


Concurrency

19

J2EE Components & Services


Components


Java Servlets


JavaServer Pages (JSP)


Enterprise JavaBeans (EJB)


Standard services & supporting technologies


Java database connectivity(JDBC) data access API


Java Messaging Service (JMS)



(Remote Method Invocations (RMI))


Extensible Markup Languages(XML)


JavaIDL (Interface Description Language)


JavaMail


Java Security


CORBA technology


Design Patterns






J2EE Components


Servlets


Servlets are the Java platform technology of choice
for extending and enhancing web servers.


Servlets provide a component
-
based, platform
-
independent method for building web
-
based
applications, without the performance limitations of
CGI programs.


Servlets have access to the entire family of Java APIs,
including the
JDBC
API to access enterprise
databases.


Servlets can also access a library of HTTP
-
specific
calls and receive all the benefits of the mature Java
language, including portability, performance, reusability,
and crash
protection.

Anatomy of a Servlet


init
()


the
init
() function is called when the servlet is
initialized by the server. This often happens on the first
doGet
() or
doPut
() call of the servlet.


destroy()


this function is called when the servlet is being
destroyed by the server, typically when the server process
is being stopped.


Anatomy of a Servlet


doGet
()


the
doGet
() function is called when the servlet
is called via an HTTP GET.


doPost
()


the
doPost
() function is called when the servlet
is called via an HTTP POST.


POSTs are a good way to get input from HTML forms


Anatomy of a Servlet


HTTPServletRequest object


Information about an HTTP request


Headers


Query String


Session


Cookies


HTTPServletResponse object


Used for formatting an HTTP response


Headers


Status codes


Cookies

Sample Servlet

import

java.io.*;




//Apache Tomcat sample code

import

javax.servlet
.*;

import

javax.servlet.http
.*;

public class

HelloWorld

extends

HttpServlet



{



public void

doGet
(
HttpServletRequest

request,
HttpServletResponse

response)



throws

IOException
,
ServletException




{



response.setContentType
("
text/html
");



PrintWriter

out =
response.getWriter
();
out.println
("
<html>
");

out.println
("
<body>
");



out.println
("
<head>
");



out.println
("
<title>Hello World!</title>
");



out.println
("
</head>
");



out.println
("
<body>
");



out.println
("
<h1>Hello World!</h1>
");



out.println
("
</body>
");



out.println
("
</html>
");



}



}

JSP


Java Server
Pages


Java Server
Pages technology uses XML
-
like tags and
scriptlets

written in the Java programming language to encapsulate the
logic that generates the content for the page.


Any and all formatting (HTML or XML) tags are passed
directly back to the response page.


By separating the page logic from its design and display and
supporting a reusable component
-
based design, JSP technology
makes it faster and easier than ever to build web
-
based
applications
.


Sample JSP

<html>





<!
-

Apache Tomcat Samples
-
>

<!
--

Copyright (c) 1999 The Apache Software Foundation. All rights reserved.
--
>

<body
bgcolor
="white">

<
jsp:useBean

id='clock' scope='page' class='
dates.JspCalendar
' type="
dates.JspCalendar
" />


<font size=4><
ul
>

<li>

Day of month: is <
jsp:getProperty

name="clock" property="
dayOfMonth
"/>

<li>

Year: is <
jsp:getProperty

name="clock" property="year"/>

<li>

Month: is <
jsp:getProperty

name="clock" property="month"/>

<li>

Time: is <
jsp:getProperty

name="clock" property="time"/>

<li>

Date: is <
jsp:getProperty

name="clock" property="date"/>

<li>

Day: is <
jsp:getProperty

name="clock" property="day"/>

<li>

Day Of Year: is <
jsp:getProperty

name="clock" property="
dayOfYear
"/>

<li>

Week Of Year: is <
jsp:getProperty

name="clock" property="
weekOfYear
"/>

<li>

era: is <
jsp:getProperty

name="clock" property="era"/>

<li>

DST Offset: is <
jsp:getProperty

name="clock" property="
DSTOffset
"/>

<li>

Zone Offset: is <
jsp:getProperty

name="clock" property="
zoneOffset
"/>

</
ul
>

</font>


</body>

</html>


EJB


Enterprise Java Beans


Enterprise
JavaBeans

is the server
-
side
component architecture for the
J2EE
platform.


EJB
enables rapid and simplified development of
distributed, transactional, secure and portable Java
applications.


Enterprise Java Beans are components that are
deployed into containers


The container provides services


Loading / Initialization


Transactions


Persistence


Communication with EJB
clients

Anatomy of an EJB


Remote Interface


Methods that can be accessed by the outside world.


Extends javax.ejb.EJBObject


Remote Home Interface


Life
-
cycle methods (create, findByPrimaryKey)


Extends javax.ejb.EJBHome which extends java.rmi.Remote


Bean class


The class performing the actual business process


Implements an interface based on type of bean

Client / EJB Relationship


How does a client application (Java class) utilize EJBs?


Lookup
-

JNDI ENC


Network protocol
-

RMI


EJB container creates object with
Remote Home
and Home interfaces


this object passes calls to the bean class

EJB


Enterprise Java Beans


Entity Beans


Session Beans


Message
Beans

EJB


Entity Beans


Entity beans are classes that map to individual entities


typically, an Entity bean references a row in a
database table, providing an object representation of
that database object.


For example, an entity bean could represent a customer, and
changing the values in that entity bean would cause updates
to that database
row

Entity Beans
-

Persistence


Container Managed Persistence (CMP)


The EJB container automatically persists the EJB objects,
usually to a relational database where each type of object is
represented as a table, and each instance of the object is a
row in that table


Bean Managed Persistence (BMP)


The EJB container calls bean methods when it is appropriate
for the bean to load, save or update data, enforcing
transactions without transaction code written by the bean
developer

EJB


Session Beans


Session
bean is a server
-
side extension of a client that
exists to service requests made by the client.


Session
beans perform work for a client application


For example, a session bean could charge a credit card for a
specific transaction
.


It
simulates an interactive session between a client and
the server
-
based
component.

Session Beans


Two States


Stateful



A
stateful

bean maintains a conversational
state with a client. The client perceives that it is only
talking to one bean, and that bean maintains
information between
calls.


Stateless


A stateless bean maintains no client
information between method calls


the container
can substitute beans as necessary between method
calls.

EJB


Session Bean Example

package
org.jboss.docs.interest
;

import
javax.ejb.EJBObject
;

import
java.rmi.RemoteException
;




/** This interface defines the `Remote' interface for the `Interest' EJB. Its single method is the only
method exposed to the outside world. The class
InterestBean

implements the method. */


public interface Interest extends
EJBObject



{


/** Calculates the compound interest on the sum `principle', with interest rate per period `rate'
over `periods' time periods. This method also prints a message to standard output; this is
picked up by the EJB server and logged. In this way we can demonstrate that the method is
actually being executed on the server, rather than the client. */




public double
calculateCompoundInterest
(double principle, double rate, double periods)
throws
RemoteException
;


}

EJB


Session Bean Example

package
org.jboss.docs.interest
;

import
java.io.Serializable
;

import
java.rmi.RemoteException
;

import
javax.ejb.CreateException
;

import
javax.ejb.EJBHome
;


/** This interface defines the 'home' interface for the 'Interest' EJB. */

public interface
InterestHome

extends
EJBHome



{



/** Creates an instance of the `
InterestBean
' class on the server, and returns a remote
reference to an Interest interface on the client. */



Interest create() throws
RemoteException
,
CreateException
;


}

EJB


Session Bean Example

package
org.jboss.docs.interest
;

import
java.rmi.RemoteException
;

import
javax.ejb.SessionBean
;

import
javax.ejb.SessionContext
;


/** This class contains the implementation for the '
calculateCompoundInterest
' method exposed by
this Bean. It includes empty method bodies for the methods prescribe by the
SessionBean

interface; these don't need to do anything in this simple example. */


public class
InterestBean

implements
SessionBean



{


public double
calculateCompoundInterest
(double principle, double rate, double periods)



{




System.out.println
("Someone called `
calculateCompoundInterest
!'");




return principle *
Math.pow
(1+rate, periods)
-

principle;



}




public void
ejbCreate
() {}


public void
ejbPostCreate
() {}


public void
ejbRemove
() {}


public void
ejbActivate
() {}


public void
ejbPassivate
() {}


public void
setSessionContext
(
SessionContext

sc
) {}


}

EJB


Session Bean Example


<?xml

version="1.0"

encoding="UTF
-
8"?>


<
ejb
-
jar>


<description>
JBoss

Interest

Sample

Application</description>


<display
-
name>Interest

EJB</display
-
name>


<enterprise
-
beans>


<session>


<
ejb
-
name>Interest</
ejb
-
name>


<home>
org.jboss.docs.interest.InterestHome
</home>


<remote>
org.jboss.docs.interest.Interest
</remote>


<
ejb
-
class>
org.jboss.docs.interest.InterestBean
</
ejb
-
class>


<session
-
type>Stateless</session
-
type>


<transaction
-
type>Bean</transaction
-
type>


</session>


</enterprise
-
beans>

</
ejb
-
jar>

EJB


Session Bean Example

package
org.jboss.docs.interest
;

import
javax.naming.InitialContext
;

import
javax.rmi.PortableRemoteObject
;


class
InterestClient



{



/** This method does all the work. It creates an instance of the Interest EJB on the EJB server,
and calls its `
calculateCompoundInterest
()' method, then prints the result of the calculation. */


public static void main(String[]
args
)


{
try
{



InitialContext

jndiContext

= new
InitialContext
();



ref =
jndiContext.lookup
("interest/Interest");



InterestHome

home = (
InterestHome
)
PortableRemoteObject.narrow
(ref,
InterestHome.class
);



Interest
interest

=
home.create
(); //Create an Interest object from the Home interface




System.out.println
(
interest.calculateCompoundInterest
(1000, 0.10, 2));



}


catch(Exception e)



{




System.out.println
(
e.toString
());



}



}


}

EJB


Message Beans


Message beans are classes that receive asynchronous
notification from a Java Message Service server


For example, a message bean could be activated when vendor
sends a purchase order to a JMS queue
.


These are stateless, server
-
side components invoked by
the EJB container on the receipt of a message in an
associated JMS Queue or Topic.


This
component allows the EJB container to provide
support for asynchronous message processing.


JMS


Java Message Service


Enterprise messaging provides a reliable, flexible
service for the asynchronous exchange of critical
business data and events throughout an enterprise.


The
JMS API adds to this a common API and provider
framework that enables the development of portable,
message based applications in the Java programming
language.



JMS


Java Message Service

JMS Queue

JMS Topic

JDBC


Data Access API


JDBC
technology is an API that lets you access virtually
any tabular data source from the
Java
programming
language.


Cross
-
DBMS connectivity to a wide range of SQL databases


Access to other tabular data sources, such as spreadsheets or
flat files.

JDBC


Driver Types


Level 1
-

A
JDBC
-
ODBC bridge

provides JDBC API access
via one or more ODBC drivers.


Level 2
-

A
native
-
API partly Java technology
-
enabled driver

converts JDBC calls into calls on the client API for
Oracle, Sybase, Informix, DB2, or other DBMS.


Level 3
-

A
net
-
protocol fully Java technology
-
enabled driver

translates JDBC API calls into a DBMS
-
independent net
protocol which is then translated to a DBMS protocol by
a server.


Level 4
-

A
native
-
protocol fully Java technology
-
enabled
driver

converts JDBC technology calls into the network
protocol used by DBMSs directly.

JNDI


Java Naming and Directory Interface


JNDI is an API specified in
Java

that
provides naming and directory
functionality to applications written in Java. It is designed especially for Java
by using Java's object model.


Using JNDI, Java applications can store and retrieve named Java objects of
any type.


JNDI provides methods for performing standard directory operations, such
as,
associating attributes with objects and searching for objects using their
attributes.


JNDI allows Java applications to take advantage of information in a variety
of existing naming and directory
services and
allows Java applications to
coexist with legacy applications and systems.


Designed to standardize access to a variety of naming and directory
services, the Java Naming and Directory Interface (JNDI) API provides a
simple mechanism for J2EE components to look up other objects they
require.

JNDI
-

Layers

JNDI


Common Uses


JNDI ENC


“enterprise naming context”


EJB lookup within a J2EE app server


Lightweight Directory Access Protocol (LDAP)
integration


Dynamic registration of services and clients


Peer to Peer computing






JTA / JTS


Transactions


The Java Transaction API (JTA) and the Java Transaction
Service (JTS) allow J2EE application servers to take the
burden of transaction management
of
the component
developer.


Developers can define the transactional properties of
Enterprise
JavaBeans
technology based components during
design or deployment using declarative statements in the
deployment descriptor.


The application server takes over the transaction
management responsibilities.

JavaMail


The
JavaMail

API
provides a set of abstract classes that model a
mail system.


The API provides a platform independent and protocol
independent framework to build Java technology
-
based mail
and messaging applications.


Supports
common mail protocols


IMAP (Internet Message Access
Protocol)


POP (Post Office Protocol)


SMTP (Simple Mail Transfer Protocol)


MIME (Multipurpose Internet Mail Extensions)

JAAS


Java Authentication and
Authorization Service


Authentication

of users, to reliably and securely determine who
is currently executing Java code, regardless of whether the
code is running as an application, an applet, a bean, or a servlet;
and


A
uthorization

of users to ensure they have the access control
rights (permissions) required to do the actions performed.






J2EE Deployment


JAR


Java ARchive


Java class file


EJBs


WAR
-

Web ARchive


Servlets


JSPs


EAR
-

Enterprise ARchive


Contains other JARs and WARs to form an entire
application


Deployment descriptors


XML


Required for EJB JARs, WARs, EARs

Questions?