Title: A plataforma J2EE
1A plataforma J2EE
2Aplicações MultiCamadas
3Comunicação com o servidor
4A Camada Web
5A 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.
6A camada de negócios e a camada da corporação
7A 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.
8A camada da corporação
- enterprise resource planning (ERP),
- mainframe transaction processing,
- database systems, and
- other legacy information systems
9A plataforma J2EE
10Os 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
11O servidor J2EE e containers
12O 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.
13Empacotamento
- 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.
14Empacotamento
- 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.
15Empacotamento
- 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.
16Empacotamento
- 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.
17Deployment Tool Empacotamento
18Para iniciar em J2EE
- Table 2 Required Environment Variables
JAVA_HOMEThe location of the J2SE SDK
installation.J2EE_HOMEThe location of the J2EE
SDK installation.PATHShould include the bin
directories of the J2EE SDK, J2SE, and ant
installations.
19Iniciando com J2EE
- Starting the J2EE Server
- j2ee -verbose
- j2ee -stop
- Starting the deploytool
- deploytool
20Criando uma Aplicação
- In the deploytool, select File -gt New-gt
Application. - Click Browse.
- In the file chooser, navigate to
j2eetutorial/examples/src/ejb/converter. - In the File Name field enter ConverterApp.ear.
- Click New Application.
- Click OK.
21Criando 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
22A interface remota
- A remote interface defines the business methods
that a client may call. - The business methods are implemented in the
enterprise bean code.
23A 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
-
24A 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.
25A 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 -
26A implementação do EJB
- This class implements the two business methods,
- dollarToYen and
- yenToEuro,
- that the Converter remote interface defines.
27A implementação do EJB
- import 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
28A 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
29A 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)
30Compilar o código fonte
- Now you are ready to compile the
- remote interface (Converter.java),
- home interface (ConverterHome.java),
- enterprise bean class (ConverterBean.java)
31Empacotamento 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-gtDescriptor Viewer.
32New Enterprise Bean Wizard
- Click Next.
- Select the Create new JAR File in Application
button. - In the combo box, select ConverterApp.
- In the JAR Display Name field enter ConverterJAR.
- Click Edit. In the tree under Available Files,
locate the ....../converter directory. - Select the following classes from the Available
Files tree and click Add Converter.class,
ConverterBean.class, ConverterHome.class. Click
OK. Click Next. - Under Bean Type, select the Session radio button.
- Select the Stateless radio button.
33New Enterprise Bean Wizard
- In the Enterprise Bean Class combo box, select
ConverterBean. - In the Enterprise Bean Name field, enter
ConverterEJB. - In the Remote Home Interface combo box, select
ConverterHome. - In the Remote Interface combo box, select
Converter. - Click Next.
- Transaction Management Dialog Box
- Because you may skip the remaining dialog boxes,
click Finish.
34Criando 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.
35Criando 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.
36Criando 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.
37Criando 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
-
38Criando um cliente da aplicação
- public class ConverterClient
-
- public static void main(String args)
- try
- Context initial new
InitialContext() - Object objref initial.lookup
- ("javacomp/env/ejb/SimpleConverter"
) -
- ConverterHome home
- (ConverterHome)PortableRemoteObject
.narrow(objref,
ConverterHome.class) -
- Converter currencyConverter
home.create() -
-
39Criando um cliente da aplicação
- public static void main(String args)
- BigDecimal param new BigDecimal
("100.00") - BigDecimal amount
- currencyConverter.dollarToYen(param)
- System.out.println(amount)
- amount currencyConverter.yenToEuro(pa
ram) - System.out.println(amount)
-
- System.exit(0)
-
- catch (Exception ex)
- System.err.println("Caught an
unexpected exception!") - ex.printStackTrace()
-
-
-
-
40Compilando 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.
41Compilando 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. - 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.
42O Cliente Web página JSP
- The classes needed by the client are declared
with a JSP page directive (enclosed within the
lt_at_ gt characters). - Because locating the home interface and creating
the enterprise bean are performed only once, they
appear in a JSP declaration (enclosed within the
lt! gt 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 lt gt
characters) retrieves a parameter from the
request and converts it to a double. - Finally, JSP expressions (enclosed within lt gt
characters) invoke the enterprise bean's business
methods and insert the result into the stream of
data returned to the client.
43O Cliente Web página JSP
- Classes usadas pelo cliente
- lt_at_ page import"Converter,ConverterHome,javax.ejb
., - javax.naming., javax.rmi.PortableRemoteObject,
- java.rmi.RemoteException" gt
44O Cliente Web página JSP
- lt! JSP Declaration
- private Converter converter null
- public void jspInit()
- try
- InitialContext ic new
InitialContext() - Object objRef ic.lookup("
- javacomp/env/ejb/TheConverter")
- ConverterHome home
- (ConverterHome)PortableRemoteObject.narro
w( - objRef, ConverterHome.class)
- converter home.create()
- catch (RemoteException ex)
- ...
-
-
- ...
- gt
45O Cliente Web página JSP
- lthtmlgt FORM
HTML - ltheadgt
- lttitlegtConverterlt/titlegt
- lt/headgt
-
- ltbody bgcolor"white"gt
- lth1gtltcentergtConverterlt/centergtlt/h1gt
- lthrgt
- ltpgtEnter an amount to convertlt/pgt
- ltform method"get"gt
- ltinput type"text" name"amount" size"25"gt
- ltbrgt
- ltpgt
- ltinput type"submit" value"Submit"gt
- ltinput type"reset" value"Reset"gt
- lt/formgt
46O Cliente Web página JSP
- lt obtém parâmetro
- String amount request.getParameter("amount")
- if ( amount ! null amount.length() gt 0 )
converte - BigDecimal d new BigDecimal (amount)
- gt
- ltpgtlt amount gt dollars are invoca o método
- lt converter.dollarToYen(d) gt Yen.
- ltpgtlt amount gt Yen are
- lt converter.yenToEuro(d) gt Euro.
- lt
-
- gt
- lt/bodygt
- lt/htmlgt
47O 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.
48Especificando 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.
49Especificando os nomes JNDI
- To map the enterprise bean references in the
clients to the JNDI name of the bean, follow
these steps - In the tree, select ConverterApp.
- Select the JNDI Names tab.
- To specify a JNDI name for the bean, in the
Application table locate the ConverterEJB
component and enter MyConverter in the JNDI Name
column. - To map the references, in the References table
enter MyConverter in the JNDI Name for each row.
50Especificando os nomes JNDI
51Finalmente...
- Efetuar o deployment da aplicação
- Executar o cliente desktop
- Executar o cliente Web