Top Banner
A plataforma J2EE
51

A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Mar 26, 2015

Download

Documents

Sean Schwartz
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A plataforma J2EE

Page 2: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Aplicações MultiCamadas

Page 3: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Comunicação com o servidor

Page 4: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A Camada Web

Page 5: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A camada Web

• J2EE Web components :– 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.

• Static HTML pages and applets – bundled with Web components during application

assembly, – but are not considered Web components by the

J2EE specification.

Page 6: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A camada de negócios e a camada da corporação

Page 7: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A camada de negócios

• 3 kinds of enterprise beans: – session bean represents a transient

conversation with a client. When the client finishes executing, the session bean and its data are gone.

– entity bean represents persistent data stored in one row of a database table. If the client terminates or if the server shuts down, the underlying services ensure that the entity bean data is saved.

– message-driven bean combines features of a session bean and a Java Message Service ("JMS") message listener, allowing a business component to receive JMS messages asynchronously.

Page 8: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A camada da corporação

• enterprise resource planning (ERP),

• mainframe transaction processing,

• database systems, and

• other legacy information systems

Page 9: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A plataforma J2EE

Page 10: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Os serviços oferecidos

• Naming Services - JNDI• Deployment Services

– Deployment Descriptor (XML files)– Deployment Units (EAR files)

• Transaction Services - JTA• Security Services - JAAS• Java Database Connectivity - JDBC• JavaMail/JAF (Java Application Framework)• Java Messaging Services – JMS• Java Api for XML Processing - JAXP• Java Connector Architecture - JCA

Page 11: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

O servidor J2EE e containers

Page 12: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

O servidor J2EE e containers• J2EE server

– The runtime portion of a J2EE product.– Provides EJB and Web containers.

• Enterprise JavaBeans (EJB) container – Manages the execution of enterprise beans.

• Web container – Manages the execution of JSP page and servlets

• Application client container – Manages the execution of application client components.

• Applet container – Consists of a Web browser and Java Plug-in running on the

client together.

Page 13: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Empacotamento

• J2EE components are packaged separately and bundled into a J2EE application for deployment.

• Each component, its related files such as GIF and HTML files or server-side utility classes, and a deployment descriptor are assembled into a module and added to the J2EE application.

• A J2EE application is composed of:– one or more enterprise bean, Web, or application

client component modules.

Page 14: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Empacotamento

• A J2EE application and each of its modules has its own deployment descriptor. – A deployment descriptor is an XML document with

an .xml extension that describes a component's deployment settings.

– An enterprise bean module deployment descriptor, for example, declares:

• transaction attributes • ecurity authorizations for an enterprise bean.

• Because deployment descriptor information is declarative, it can be changed without modifying the bean source code.

Page 15: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Empacotamento

• A J2EE application with all of its modules is delivered in an Enterprise Archive (EAR) file.– An EAR file is a standard Java Archive

(JAR) file with an .ear extension.

• In the GUI version of the J2EE SDK application deployment tool, you create an EAR file first and add JAR and Web Archive (WAR) files to the EAR.

Page 16: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Empacotamento

• Each EJB JAR file contains a deployment descriptor, the enterprise bean files, and related files.

• Each application client JAR file contains a deployment descriptor, the class files for the application client, and related files.

• Each WAR file contains a deployment descriptor, the Web component files, and related resources.

Page 17: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Deployment Tool Empacotamento

Page 18: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Para iniciar em J2EE

• Table 2 Required Environment Variables JAVA_HOME

The location of the J2SE SDK installation.J2EE_HOME

The location of the J2EE SDK installation.PATH

Should include the bin directories of the J2EE SDK, J2SE, and ant installations.

Page 19: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Iniciando com J2EE

•Starting the J2EE Server – j2ee [-verbose]– j2ee -stop

•Starting the deploytool –deploytool

Page 20: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Criando uma Aplicação

1. In the deploytool, select File -> New-> Application.

2. Click Browse.

3. In the file chooser, navigate to j2eetutorial/examples/src/ejb/converter.

4. In the File Name field enter ConverterApp.ear.

5. Click New Application.

6. Click OK.

Page 21: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Criando um enterprise bean

• The enterprise bean in our example is a stateless session bean called ConverterEJB.

• Coding the Enterprise Bean – Remote interface – Home interface – Enterprise bean class

Page 22: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A interface remota

• A remote interface defines the business methods that a client may call.

• The business methods are implemented in the enterprise bean code.

Page 23: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A interface remota

import javax.ejb.EJBObject;import java.rmi.RemoteException;import java.math.*;

public interface Converter extends EJBObject {

public BigDecimal dollarToYen (BigDecimal dollars)

throws RemoteException; public BigDecimal yenToEuro(BigDecimal yen) throws RemoteException;

}

Page 24: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A interface Home

• A home interface defines the methods that allow a client to create, find, or remove an enterprise bean.

• The ConverterHome interface contains a single create method, which returns an object of the remote interface type.

Page 25: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A interface Home

import java.io.Serializable;import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.EJBHome;

public interface ConverterHome extends EJBHome {

Converter create ( ) throws RemoteException, CreateException;

}

Page 26: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A implementação do EJB

This class implements the two business methods,

• dollarToYen and • yenToEuro,

that the Converter remote interface defines.

Page 27: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A implementação do EJBimport java.rmi.RemoteException; import javax.ejb.SessionBean;import javax.ejb.SessionContext;import java.math.*;

public class ConverterBean implements SessionBean { BigDecimal yenRate = new BigDecimal("121.6000"); BigDecimal euroRate = new BigDecimal("0.0077");

public BigDecimal dollarToYen(BigDecimal dollars) { BigDecimal result = dollars.multiply(yenRate); return result.setScale(2,BigDecimal.ROUND_UP); } o o o

Page 28: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A implementação do EJB

public class ConverterBean implements SessionBean { o o o public BigDecimal dollarToYen(BigDecimal dollars) { BigDecimal result = dollars.multiply(yenRate); return result.setScale(2,BigDecimal.ROUND_UP); }

public BigDecimal yenToEuro(BigDecimal yen) { BigDecimal result = yen.multiply(euroRate); return result.setScale(2,BigDecimal.ROUND_UP); } o o o

Page 29: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

A implementação do EJB

public class ConverterBean implements SessionBean {

o o o public ConverterBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext

sc) {}}

Page 30: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Compilar o código fonte

Now you are ready to compile the • remote interface

(Converter.java), • home interface

(ConverterHome.java),• enterprise bean class

(ConverterBean.java)

Page 31: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Empacotamento do EJB

New Enterprise Bean Wizard of the deploytool. During this process the wizard:

• Creates the bean's deployment descriptor. • Packages the deployment descriptor and

the bean's classes in an EJB JAR file. • Inserts the EJB JAR file into the application's

ConverterApp.ear file.

• During the packaging process you can view the deployment descriptor by selecting Tools->Descriptor Viewer.

Page 32: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

New Enterprise Bean Wizarda. Click Next. b. Select the Create new JAR File in Application

button. c. In the combo box, select ConverterApp. d. In the JAR Display Name field enter ConverterJAR. e. Click Edit. In the tree under Available Files, locate

the ....../converter directory. f. Select the following classes from the Available

Files tree and click Add: Converter.class, ConverterBean.class, ConverterHome.class. Click OK. Click Next.

g. Under Bean Type, select the Session radio button.

h. Select the Stateless radio button.

Page 33: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

New Enterprise Bean Wizard

i. In the Enterprise Bean Class combo box, select ConverterBean.

j. In the Enterprise Bean Name field, enter ConverterEJB.

k. In the Remote Home Interface combo box, select ConverterHome.

l. In the Remote Interface combo box, select Converter.

m. Click Next. n. Transaction Management Dialog Box o. Because you may skip the remaining dialog

boxes, click Finish.

Page 34: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Criando um cliente da aplicação

• A J2EE application client is a program written in the Java programming language.

• At run time, the client program executes in a different virtual machine (VM) than the J2EE server.

Page 35: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Criando um cliente da aplicação

• The J2EE application client in this example requires two different JAR files.

• The first JAR file is for the J2EE component of the client. – This JAR file contains the client's deployment

descriptor and its class files. When you run the New Application Client wizard, the deploytool automatically creates the JAR file and stores it in the application's EAR file.

Page 36: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Criando um cliente da aplicação

• The second JAR file contains stub classes that are required by the client program at run time.

• These stub classes enable the client to access the enterprise beans that are running in the J2EE server.

Page 37: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Criando um cliente da aplicação

import javax.naming.Context;import javax.naming.InitialContext;import javax.rmi.PortableRemoteObject;

import java.math.BigDecimal;

import Converter;import ConverterHome;

Page 38: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Criando um cliente da aplicaçãopublic class ConverterClient {

public static void main(String[] args) { try { Context initial = new InitialContext(); Object objref = initial.lookup ("java:comp/env/ejb/SimpleConverter");

ConverterHome home =

(ConverterHome)PortableRemoteObject.narrow(objref, ConverterHome.class);

Converter currencyConverter = home.create();

Page 39: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Criando um cliente da aplicaçãopublic static void main(String[] args) { BigDecimal param = new BigDecimal ("100.00"); BigDecimal amount = currencyConverter.dollarToYen(param); System.out.println(amount); amount = currencyConverter.yenToEuro(param); System.out.println(amount);

System.exit(0);

} catch (Exception ex) { System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); } } }

Page 40: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Compilando e Empacotando

Compiling the Application Client The application client files are compiled at the same time

as the enterprise bean files, as described in Compiling the Source Files.

Packaging the J2EE Application Client To package an application client component, you run the

New Application Client Wizard of the deploytool. During this process the wizard:

• Creates the application client's deployment descriptor. • Puts deployment descriptor and client files into a JAR

file. • Adds the JAR file to the application's ConverterApp.ear

file.

Page 41: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Compilando e EmpacotandoCompiling the Application Client The application client files are compiled at the same time as the

enterprise bean files, as described in Compiling the Source Files.

Packaging the J2EE Application Client To package an application client component, you run the New

Application Client Wizard of the deploytool. During this process the wizard:

• Creates the application client's deployment descriptor. • Puts deployment descriptor and client files into a JAR file. • Adds the JAR file to the application's ConverterApp.ear file. Specifying the Application Client's Enterprise Bean

Reference • When it invokes the lookup method, the ConverterClient refers

to the home of an enterprise bean: Object objref = myEnv.lookup("ejb/SimpleConverter"); You specify this reference in the deployment tool.

Page 42: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

O Cliente Web – página JSP

The classes needed by the client are declared with a JSP page directive (enclosed within the <%@ %> characters).

Because locating the home interface and creating the enterprise bean are performed only once, they appear in a JSP declaration (enclosed within the <%! %> characters), that contains the initialization method, jspInit, of the JSP page.

The declaration is followed by standard HTML markup for creating a form with an input field.

A scriptlet (enclosed within the <% %> characters) retrieves a parameter from the request and converts it to a double.

Finally, JSP expressions (enclosed within <%= %> characters) invoke the enterprise bean's business methods and insert the result into the stream of data returned to the client.

Page 43: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

O Cliente Web – página JSP

Classes usadas pelo cliente

<%@ page import="Converter,ConverterHome,javax.ejb.*,

javax.naming.*, javax.rmi.PortableRemoteObject,

java.rmi.RemoteException" %>

Page 44: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

O Cliente Web – página JSP

<%! JSP Declaration private Converter converter = null; public void jspInit() { try { InitialContext ic = new InitialContext(); Object objRef = ic.lookup(" java:comp/env/ejb/TheConverter"); ConverterHome home = (ConverterHome)PortableRemoteObject.narrow( objRef, ConverterHome.class); converter = home.create(); } catch (RemoteException ex) { ... } } ...%>

Page 45: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

O Cliente Web – página JSP

<html> FORM HTML<head> <title>Converter</title></head>

<body bgcolor="white"><h1><center>Converter</center></h1><hr><p>Enter an amount to convert:</p><form method="get"><input type="text" name="amount" size="25"><br><p><input type="submit" value="Submit"><input type="reset" value="Reset"></form>

Page 46: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

O Cliente Web – página JSP

<% obtém parâmetro String amount = request.getParameter("amount"); if ( amount != null && amount.length() > 0 ) {

converte BigDecimal d = new BigDecimal (amount);%> <p><%= amount %> dollars are invoca o método <%= converter.dollarToYen(d) %> Yen. <p><%= amount %> Yen are <%= converter.yenToEuro(d) %> Euro.<% }%></body></html>

Page 47: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

O Cliente Web – página JSP

• A compilação é automática pelo container (servidor web apropriado para JSP).

• É necessário fazer o empacotamento via deployment tool.

Page 48: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Especificando os nomes JNDI• Although the J2EE application client and

the web client access the same enterprise bean, their code refers to the bean's home by different names. – The J2EE application client refers to the

bean's home as ejb/SimpleConverter, – but the web client refers to it as

ejb/TheConverter.

• These references are in the parameters of the lookup calls. In order for the lookup method to retrieve the home object, you must map the references in the code to the enterprise bean's JNDI name.

Page 49: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Especificando os nomes JNDI

To map the enterprise bean references in the clients to the JNDI name of the bean, follow these steps:

1. In the tree, select ConverterApp. 2. Select the JNDI Names tab. 3. To specify a JNDI name for the bean, in

the Application table locate the ConverterEJB component and enter MyConverter in the JNDI Name column.

4. To map the references, in the References table enter MyConverter in the JNDI Name for each row.

Page 50: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Especificando os nomes JNDI

Page 51: A plataforma J2EE. Aplicações MultiCamadas Comunicação com o servidor.

Finalmente...

• Efetuar o deployment da aplicação

• Executar o cliente desktop

• Executar o cliente Web