Download Ch10 EJB Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 10 EJB
• Concepts of EJB
• Three Components in Creating an EJB
• Starting/Stopping J2EE Server and Deployment Tool
• Installation and Configuration of J2EE
• Concept of Deployment
• Steps in Developing an EJB Applications
• A simple Example
• Entity Beans and Session Beans
• Example of an Entity Bean Application
• Example of an Session Bean Application
• In-class Exercises
Concepts of EJB
• What is an EJB?
– Part of J2EE (Java 2 Enterprise Edition)
– Three-tier or multi-tier client/server applications
– Distributed applications
• Why EJB?
– Components-based architecture makes application
development easier and faster
– Platform independent
– Developers concentrate on business logic rather than
presentation
– GUI deployment tools make packaging and deploying an EJB
application easier and faster
– Integrated Java technology
– Hidden-details in Client/server communications and JDBC
connectivity
Concepts of EJB (continue)
• Multi-tiered Applications
Concepts of EJB (continue)
• J2EE server communications with other tiers
Concepts of EJB (continue)
• Closer look of Web tier
Concepts of EJB (continue)
• Closer look of Business and EIS (Enterprise Info Sys) tier
Concepts of EJB (continue)
• J2EE Server and Containers
Three Components in Creating an EJB
• Coding the remote interface
– define the business methods that a client may call, i.e, retrieve
a message or compute data
– name convention of the code: ProgRemote.java
• Coding the home interface
– define the methods that allow a client to create, find, or remove
an EJB
– name convention of the code: ProHome.java
• Coding the bean
– implement the methods of the remote interface. The methods
will be called by the container-generated code after the
deployment
– name convention of the code: ProEJB.java or ProBean.java
• Note: to use the EJB, one must create a client code
Installation and Configuration of J2EE
•
•
•
•
Download J2EE from http://java.sun.com
J2EE requires J2SDK to be installed
Follow the instruction to install J2EE
Setup environment variables (for example)
– set JAVA_HOME=c:\j2sdk1.4.1
– set J2EE_HOME=c:\j2sdkee1.3.1
• Other links
– J2EE API: http://java.sun.com/api.html
– J2EE tutorial:
http://java.sun.com/search/index.jsp?tab=jsc&docs=tutorial&w
ord=EJB
– general topic tutorials: http://java.sun.com/docs/books/tutorial
Starting/Stopping J2EE Server and Deployment
Tool
• To start the server, click on j2ee.bat in the
J2EE_HOME\bin directory, or type the following
command in command console:
start %J2EE_HOME%\bin\j2ee -verbose
• To start the deployment tool, click on deploytool.bat in
the J2EE_HOME\bin directory, or type the following
command in command console:
start %J2EE_HOME%\bin\deploytool
• To stop the server, use the following command
%J2EE_HOME%\bin\j2ee -stop
Application Deployment Tool in J2EE
• deploytool provides GUI for assembling, verifying,
packaging, configuring, and deploying J2EE applications
• It manages your development and production
environments
• It creates a JAR file, ProEJB.jar, for the EJB, and an
application file, Pro.ear, for development in the EJB
container
• It will also create additional files needed for
communications with the clients, and a deployment
descriptor
Steps in Developing an EJB Applications
• Step 1: Create the EJB
– code the remote interface (ProRemote.java)
– code the home interface (ProHome.java)
– code the bean (ProgEJB.java)
– compile all three components using the following
command:
javac -classpath .;%J2EE_HOME%\lib\j2ee.jar Pro*.java
• Step 2: Start J2EE Server (see previous slide)
• Step 3: Deploy the EJB Using deploytool (see previous
slide)
• Step 4: Create and compile a client code
• Step 5: Execute the client code
A simple Example
• (p. 389) HelloRemote.java - define the business method
as remote interface to retrieve hello message
• (p. 390) HelloHome.java - define the method that allow a
client to create, find, or remove an EJB
• (p. 391) HelloEJB.java - implement the methods of the
remote interface as SessionBean, more specifically, return
the hello message
• To compile:
javac -classpath .;%J2EE_HOME%\lib\j2ee.jar Hello*.java
• To deploy the Hello application, follow the steps on p. 8
of newupdatech10.doc
A simple Example (continue)
• (p. 393) HelloClient.java - a client application using the
EJB to display the message
• To execute the client application:
- compile the HelloClient.java
javac -classpath .;%J2EE_HOME%\lib\j2ee.jar HelloClient.java
- execute the application:
java -classpath .;%J2EE_HOME%\lib\j2ee.jar;HelloClient.jar
HelloClient
Entity Beans and Session Beans
• Entity beans model business objects such as customers,
orders, and prodcuts
• Entity beans have a persistent representation, usually in a
database; J2EE server uses Cloudscape database
• Session beans perform operations on the business objects
such as place an order, or reserve a flight
• A session bean presents a client in the J2EE server
• The client invokes the session bean’s methods to access an
application deployed in the server
• Session beans are not persistent.
• Session beans can be stateless or stateful
– a stateful bean saves information so it can interact with the client
– a stateless bean requires no continuing interaction with the client
Entity Beans
• Entity beans model business objects such as
customers, orders, and products
• Entity beans have a persistent representation, usually
in a database; J2EE server uses Cloudscape database
• Entity beans can be Container-Managed Persistence
(CMP) or Bean-Managed Persistence (BMP)
• In CMP, the persistence of a bean’s state to the
underlying database is automatically managed by
the container
• In MBP, the programmers create code in the bean to
manage the persistence
Entity Beans (continue)
• As in developing a session bean, we need to write the Home
interface, the Remote interface, and the implementation of
the entity bean
• As in developing a session bean, we need also to write the
client code as a particular application to use the entity bean
• In the Remote interface we extend EJBObject and declare
the get() and set() methods as the business methods (see
CustomerRemote.java)
• In the Home interface, we extend EJBHome and declare the
required Create() method, the required
findByPrimaryKey(), and other finder methods (see
CustomerHome.java)
Entity Beans (continue)
• In the implementation we implement EntityBean,
the methods declared in the interfaces, and other
methods (see CustomerEJB.java in p. 397)
• After we deploy the entity bean, the find() methods
become as SQL queries generated by the container
automatically. We may modify them if it’s
necessary
• In the developing the client applications, multiple
clients can access an entity bean, the persistence and
synchronization are automatically managed
Entity Beans (continue)
• In a client code, it looks up the bean, may create
database table, call the finder methods, or get()
and set() methods to do the transactions and
manipulations of the data stored in the database
(see CustomerClient.java)
• Steps in deployment of the entity bean (see p. 18
in newupdatech10.doc)
• Steps in deployment of the client (see p. 20 in
newupdatech10.doc)
• Steps in the execution (see p. 21 in
newupdatech10.doc)
Example of an Entity Bean Application
• (p. 395) CustimerRemot.java - remote interface to define
the business methods for customer EJB
• (p. 396) CustomerHome.java - define the method that
allow a client to create and find a customer
• (p. 397) CustomerEJB.java - entity bean representing a
customer (See p. 16 in newupdatech10.doc)
• (p. 402) CustomerClient.java - client code to create
customers and test out the find method (See p. 21 in
newupdatech10.doc)
• Deployment steps (see p. 18-21 in newupdatech10.doc)
• Note: Steps may be vary depending upon the different
versions of J2EE
Example of an Session Bean Application
• A session bean may be stateful in which it holds the
client state across the method calls
• A session bean may be deployed to the entity bean, so
the data in the entity bean can be used
• Agent bean example has these features above
• (p. 404) AgentRemote.java - define the business method
for agent EJB
• (p. 404) AgentHome.java - define the methode to create
an agent bean
• (p. 405) AgentEJB.java - a stateful entity bean to place
an order
Example of an Session Bean Application
(continue)
• (p. 408/p. 29) SalesClient.java - client code to use
agent bean to retrieve the required customer names
• Deployment steps (see p. 28-29 in
newupdatch10.doc)
Using EJB in Servlet and JSP Clients
• For a Web application, we can include the web components
from J2EE application, so the clients can access EJB from
the web and dynamically update the web
• The client can be a servlet client, or JSP client.
• As a servlet client, we can write the HTML file to invoke
the J2EE application in which it will invoke the packaged
servlet
• As a JSP client, we can write a JSP code that focuses on the
presentation of the form shown in the client’s web, then we
can write a Java program to focus on the looking up the
bean and the business methods
• In either way, we package the servlet or JSP in J2EE, then
we deploy the application using deployment tools
Using EJB in Servlet and JSP
Clients(continue)
• Examples to develop a servlet client to access the Agent
Bean:
– Develop a HTML that calls the J2EE application “SalesAlias”
that invokes a sales servlet, and passes Customer ID and
Salesperson ID to the servlet using the POST method (see
updatch10.doc – SalesServlet.html)
– Develop a servlet program that connects with the Agent bean,
updates the client’s web, displays a form, creates object of Agent
bean with the table having Item number, Quantity, and Price
entered by the client, and displays the records in the table to the
client’s web (see updatech10.doc – SalesServlet.java)
– Compile the servlet program using the command:
javac -classpath .;c:\j2sdkee1.3.1\lib\j2ee.jar SalesServlet.java
– Start J2EE server, Cloudscape server, and deploytool
Using EJB in Servlet and JSP
Clients(continue)
– Package the servlet (see newupdatech10.doc – package the
servlet)
– Deploy the application (see newupdatech10.doc – the next
page)
– Execute the application
http://localhost:8000/SalesContextRoot/SalesServlet.html
(The HTML will invoke the EJB application “SalesAlias” after
user click on the Submit Query button)
Using EJB in Servlet and JSP
Clients(continue)
• Examples to develop a JSP client to access the
Customer Bean (CustomerEJB):
– Develop a Java program that looks up the Customer
bean and perform the business logics (set() and get()
methods, see newupdatech10.doc – CustomerBean.java)
– Compile the Java code using the command:
javac –classpath
.;%j2ee_home%\bin\j2ee.jar;CustomerClient.jar
CustomerBean.java
Using EJB in Servlet and JSP
Clients(continue)
– Develop a JSP program that will present a form in the client’s
web, call the methods defined in the Java code, and interact
between client and the server using the EJB (see
newupdatech10.doc – Customer.jsp)
– Start J2EE server, Cloudscape server, and deploytool
– Package the JSP (see newupdatech10.doc – Package the JSP and
JavaBean)
– Deploy the application (see newupdatech10.doc – Deploy the
Application)
– Execute
Open a Web browser, and type:
http://localhost:8000/CustomerContextRoot/Customer.jsp
In-class Exercises
- Test examples HelloXXX, CustomerXXX,
AgentXXX, and SalesXXX.
- Try-It-Yourself 3, 5, 9 and 11