Download No Slide Title

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 17
An introduction to
Enterprise JavaBeans
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 1
Objectives
Knowledge
 Describe the benefits that you get from using EJBs.
 Describe the difficulties that are associated with using EJBs.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 2
The components of a web application that uses
EJBs
Client
Browser
HTTP request
HTTP response
Server
Web server
Servlet and JSP engine
EJB server
Database server
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 3
An introduction to EJBs
 An Enterprise JavaBean, or EJB, is a server-side component that
contains the code for the business logic and data access layers of
an application.
 An EJB server, or EJB container, is an application server that
presents EJBs to the rest of the application. In addition, an EJB
server automatically provides many services for EJBs. This can
improve the performance of an application.
 Some EJB servers have a built-in mechanism for providing
persistence while other EJB servers can be configured to work
with a database management system like MySQL or Oracle.
 The Java 2 Platform, Enterprise Edition, or J2EE, provides the
classes necessary for working with EJBs.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 4
What EJBs can provide
Connection pooling. An EJB container can create a connection
pool that provides access to your database.
Persistence. An EJB container can automatically handle saving and
retrieving enterprise beans from a database.
Object pooling. An EJB container can create a pool of EJBs that
can be used by your application.
Transactions. An EJB container can manage transactions.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 5
The pros of EJBs
 Once the EJB container is configured properly, it provides many
services that can be used by the EJBs.
 Once the EJBs have been developed, the application programming
is easier than it is in an equivalent servlet/JSP application.
 Once you deploy an EJB application, it may be easier to
administer than an equivalent servlet/JSP application.
 EJBs are also available from third-party vendors. This can help
you develop applications more rapidly.
 When you use EJBs, your programmers can specialize in certain
aspects of application development.
The cons of EJBs
 EJB applications are significantly more difficult to configure and
develop than the equivalent servlet/JSP applications.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 6
Typical EJB developer roles
EJB container provider. The organization or vendor that provides
the EJB server and container.
Bean developer. A programmer who writes EJBs. For a large
application, there may be several enterprise bean developers.
Application assembler. A programmer who assembles the
enterprise beans written by in-house or third party bean developers
to create an application.
System administrator. The administrator who monitors and
maintains an EJB application after it has been deployed.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 7
Two types of EJBs
Session beans. Session beans implement the business logic of the
application. These beans exist for a client as long as the client has an
active session.
Entity beans. Entity beans implement the data access layer of an
application.
Two types of session beans
Stateless session beans. The EJB container doesn’t associate a
stateless bean with a specific client. As a result, any client can call
any instance of this type of bean.
Stateful session beans. The EJB container associates a stateful bean
with a specific client. This is similar to how servlets and JSPs
associate a session object with a specific client.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 8
Two types of persistence for entity beans
Bean-managed persistence (BMP). To use BMP, each bean must
contain SQL code for creating, finding, storing, and removing itself
from the database.
Container-managed persistence (CMP). When you use CMP, the
EJB container can automatically create, find, store, and remove each
bean from the database.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 9
The components of a User entity bean
Client
EJB server
User
extends EJBObject
UserHome extends
EJBHome
UserBean
implements EntityBean
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 10
How to implement a User entity bean
 Create a table in the database that corresponds to the data in the
entity bean.
 Code the remote interface (User) that defines the business methods
of the bean.
 Code the home interface (UserHome) that defines the methods for
creating, locating, and destroying the bean.
 Code the entity bean implementation class (UserBean).
 Package the interfaces and class in a JAR file by using a
deployment descriptor that comes with the EJB container.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 11
The remote interface for the User bean
import javax.ejb.*;
import java.rmi.RemoteException;
public interface User extends EJBObject {
public String getFirstName() throws RemoteException;
public void setFirstName(String first)
throws RemoteException;
public String getLastName() throws RemoteException;
public void setLastName(String last)
throws RemoteException;
public String getEmailAddress()
throws RemoteException;
public void setEmailAddress(String email)
throws RemoteException;
}
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 12
The home interface for the User bean
import javax.ejb.*;
import java.rmi.RemoteException;
public interface UserHome extends EJBHome {
public User create(String emailAddress)
throws RemoteException, CreateException;
public User findByPrimaryKey(String emailAddress)
throws RemoteException, FinderException;
}
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 13
How to code the remote and home interfaces
 To create a remote interface for a bean, you extend the EJBObject
interface. Then, you define the set and get methods for the bean.
 It’s a common coding practice to name the remote interface as you
would name the object in an application.
 To create the home interface for a bean, you extend the EJBHome
interface. Then, you define methods that create, locate, and destroy
beans.
 The home interface can define create methods that create a new
entity bean. Each create method returns the remote interface data
type.
 The home interface can define find methods that locate a specific
instance of a bean. Each find method returns the remote interface
data type.
 Every home interface inherits a set of remove methods from the
EJBHome interface that destroy a bean instance.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 14
The User bean class
import javax.ejb.*;
import java.rmi.RemoteException;
public class UserBean implements EntityBean {
transient private EntityContext ejbContext;
public String firstName;
public String lastName;
public String emailAddress;
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 15
The User bean class (continued)
public String getFirstName(){ return firstName; }
public void setFirstName(String first){
firstName = first;
}
public String getLastName(){ return lastName; }
public void setLastName(String last){
lastName = last;
}
public String getEmailAddress(){return emailAddress;}
public void setEmailAddress(String email){
emailAddress = email;
}
public User ejbCreate(String email) {
emailAddress = email;
return null;
}
public void ejbPostCreate(String email) { }
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 16
The User bean class (continued)
public void setEntityContext(EntityContext ctx){
ejbContext = ctx;
}
public void unsetEntityContext() {
ejbContext = null;
}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbLoad() {}
public void ejbStore() {}
public void ejbRemove() {}
}
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 17
How to code the bean implementation class
 To code an entity bean class, you implement the EntityBean
interface. In addition to the methods defined in the EntityBean
interface, the bean class must implement all methods defined in
the remote interface.
 The bean implementation class must contain an ejbCreate method
for each create method defined in the home interface. Each
ebjCreate method should contain the same number and type of
parameters as the corresponding create method.
 To learn about the context of the bean, such as transaction status
or remote references, you can use the EntityContext object. Since
it’s declared as transient, this variable isn’t part of the persistent
state of the bean.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 18
Methods used in bean-managed persistence
public
//
}
public
//
}
public
//
}
public
//
}
public
void ejbCreate() {
writes a new record to the database
void ejbLoad() {
reads data from the database
void ejbStore() {
writes data to the database
void ejbRemove() {
deletes data from the database
String ejbFindByPrimaryKey(String primaryKey)
throws ObjectNotFoundException{
// locates a record in the database
// and returns the primary key
}
private Connection getConnection() {
//a helper method that returns a connection object
}
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 19
An example of an ejbLoad method for BMP
public void ejbLoad()
Connection connection;
try {
String primaryKey =
(String)ejbContext.getPrimaryKey();
connection = this.getConnection();
Statement statement = connection.createStatement(
"SELECT * FROM User " +
"WHERE emailAddress = " + primaryKey);
ResultSet result = statement.executeQuery();
if (result.next()) {
firstName = result.getString("FirstName");
lastName = result.getString("LastName");
emailAddress =
result.getString("EmailAddress");
}
}
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 20
The ejbLoad method for BMP (continued)
catch (SQLException sqle) {
throw new EJBException(sqle);
}
finally {
if (connection != null)
connection.close();
}
}
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 21
How to implement BMP
 To implement bean-managed persistence, the bean class must
code the methods presented above so that they perform the
database access logic.
 For each find method defined in the home interface, you must
code an ejbFind method in the bean class that locates the bean in
the database and returns the primary key.
 Since many of these methods don’t accept or return data types,
you must use the EntityContext object to obtain the primary key
for the bean.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 22
Code that creates an EJB
UserHome home = //get reference to home interface
User user = home.create("[email protected]");
user.setFirstName("Andrea");
user.setLastName("Steelman");
Code that accesses an existing EJB
User previousUser =
home.findByPrimaryKey("[email protected]");
String lastName = previousUser.getLastName();
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 23
How to access an EJB in an application
 To use an EJB in an application, you must obtain a reference to the
home interface. To do this, you use networking methods.
 To create a new bean, you use one of the create methods defined in
the home interface to return an object of the remote interface type.
 To access an existing bean, you use one of the find methods
defined in the home interface to return an object of the remote
interface type.
 With EJBs, you can create and manipulate the beans without
worrying about updating the database. This is automatically done
for you.
Java Servlets and JSPCH17
© 2003, Mike Murach & Associates, Inc.
Slide 24