Enterprise Java Beans (EJB) : T.Y.I.T Sem-V Enterprise Java
Enterprise Java Beans (EJB) : T.Y.I.T Sem-V Enterprise Java
Enterprise Java Beans (EJB) : T.Y.I.T Sem-V Enterprise Java
Following are the unique characteristics that distinguish a JavaBean from other Java classes −
It provides a default, no-argument constructor.
It should be serializable and that which can implement the Serializable interface.
It may have a number of properties which can be read or written.
It may have a number of "getter" and "setter" methods for the properties.
JavaBeans Properties
A JavaBean property is a named attribute that can be accessed by the user of the object . The attribute
can be of any Java data type, including the classes that you define.
A JavaBean property may be read, write, read only, or write only. JavaBean properties are accessed
through two methods in the JavaBean's implementation class −
S.No Method & Description
.
1
getPropertyName()
For example, if property name is firstName, your method name
would be getFirstName() to read that property. This method is
called accessor.
2
setPropertyName()
For example, if property name is firstName, your method name
would be setFirstName() to write that property. This method is
called mutator.
INTRODUCTION TO EJB
BENEFITS OF EJB
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
JAVA EE CLIENTS:
Web Clients:
A web client consists of two parts:
i. dynamic web pages that contain various types of markup
languages (HTML, XML, and so on), which are generated by
web components running in the web tier, and
ii. a web browser, which renders the pages received from the
server
Applets:
A web page received from the web tier can include an
“embedded applet”. An applet is a small client application written in
the Java programming language that executes in the Java Virtual
Machine installed in the web browser. However, client systems will
likely need the Java Plug-in, and possibly a security policy file, for
the applet to successfully execute in the web browser.
Application Clients:
An application client runs on a client machine. The
application client provides a better method for users to handle
tasks, which require a richer user interface than can be provided by
a markup language. The application client typically has a graphical
user interface (GUI) created from the Swing or the Abstract Window
Toolkit (AWT) API. However, a command-line interface is certainly
possible.
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
OBJECTIVES
• Session Beans:
A Session Bean represents a transient conversation with a
client. When the Client completes its execution, the Session Bean
and it’s data are gone.
Entity Bean:
An entity bean is a remote object that manages persistent
data, performs complex business logic, potentially uses
several dependent Java objects, and can be uniquely
identified by a primary key.
Entity beans are normally coarse-grained persistent objects,
in that they utilize persistent data stored within several fine-
grained persistent Java objects.
• Message-driven Beans:
A Message-driven Bean combines features of a session
bean and a message listener, allowing a business component to
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
SESSION BEANS:
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
1. Does Not Exist to Ready: The client initiates the life cycle by
obtaining a reference to a Stateless Session Bean. The container
performs any dependency injection, and then invokes the method
annotated @PostConstruct, if any. The client can now invoke the
business methods of the bean.
2. Ready to Does Not Exist: At the end of the session bean life
cycle, the EJB container calls the method annotated @PreDestroy,
if any. The Bean instance is then ready for garbage collection.
Callback Methods:
• @PostConstruct: The container invokes this method on newly
constructed Bean instances after all dependency injections are
completed, and before the first business method is invoked on
the Enterprise Bean.
• @PreDestroy: These methods are invoked after any method
annotated @Remove is completed, and before the container
removes the Enterprise Bean instance
1. Does Not Exist to Ready: The Client initiates the life cycle by
obtaining a reference to a Stateful Session Bean. Dependency
injection is performed by container, and then invokes the
method annotated @PostConstruct, if any. The client can now
invoke the business methods of the bean.
2. Ready to Passive: In the Ready state, the EJB container may
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
Callback methods:
Given below are the annotations with the help of which you can
declare Bean Class methods as Life Cycle Callback methods:
• javax.annotation.PostConstruct
• javax.annotation.PreDestroy
• javax.ejb.PostActivate
• javax.ejb.PrePassivat
1. @PostConstruct: The container calls these methods on newly
constructed Bean instances after all dependency injections are
completed and before the first business method is invoked on
the Enterprise Bean.
2. @PreDestroy: These methods are called after any method
annotated with @Remove has completed its execution, and
before the container removes the Enterprise Bean instance.
3. @PostActivate: The container calls these methods after it moves
the Bean from secondary storage to the memory, i.e. Active
state.
4. @PrePassivate: The container calls these methods before it
passivates the Enterprise Bean. This means before the
container shifts the bean from memory to secondary storage.
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
• What information the bean wants to hold about the client across
method invocation.
• When the bean works as the mediator between the client and
the other component of the application.
• When the bean has to manage the work flow of several other
enterprise beans
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
MESSAGE-DRIVEN BEAN:
JMS Concept:
• What is Message?
Message is a unit of information or data which can be sent from
one processing computer/application to other/same
computer/applications.
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
• What is Messaging?
Messaging is a method of communication between software
components or applications.
• What is JMS?
The Java Message service is a client-side API for accessing
messaging systems.
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
Isolation
The parameters of remote calls are more isolated than those
of local calls. With remote calls, the client and the bean operate on
different copies of a parameter object. If the client changes the
value of the object, the value of the copy in the bean does not
change. This layer of isolation can help protect the bean if the client
accidentally modifies the data.
In a local call, both the client and the bean can modify the
same parameter object. In general, you should not rely on this side
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
Annotations Of EJB:
Name Description
Sr.
No
.
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
interfaces.
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
<body>
<form method="get"
action="http://localhost:8080/StateLessEJB/ConverterServlet">
<b><h2>Converter Bean</h2></b>
<table border=2>
<tr>
<td>Enter Amount</td>
<td><input type="Text" name=txtnum></td>
</tr>
<tr>
<td><input type=Submit name=cmdsubmit></td>
<td><input type=Reset name=cmdreset></td>
</tr>
</table>
</form>
</body>
</html>
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
ConverterBeanRemote.java
package server;
import java.math.BigDecimal;
import javax.ejb.Remote;
@Remote
public interface ConverterBeanRemote
{
public BigDecimal dollarToYen(BigDecimal dollars);
public BigDecimal yenToEuro(BigDecimal yen);
}
ConverterBean.java
package server;
import javax.ejb.Stateless;
import java.math.BigDecimal;
@Stateless
public class ConverterBean
{
private BigDecimal euroRate = new BigDecimal("0.0070");
private BigDecimal yenRate = new BigDecimal("112.58");
public BigDecimal dollarToYen(BigDecimal dollars)
{
BigDecimal result = dollars.multiply(yenRate);
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
ConverterServlet.java
package server;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@W ebServlet(name="ConverterServlet",
urlPatterns={"/ConverterServlet"})
public class ConverterServlet extends HttpServlet {
String str=request.getParameter("txtnum");
int number=Integer.parseInt(str);
BigDecimal num=new BigDecimal(number);
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
CartBeanRemote.java
package server;
import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface CartBeanRemote{
public void addItem(String item);
public void removeItem(String item);
public Collection getItems();
}
CartBean.java
package server;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
@Stateful
public class CartBean implements CartBeanRemote
{
private ArrayList items;
@PostConstruct
public void initialize() {
items = new ArrayList();
}
@Override
public void addItem(String item) {
items.add(item);
}
@Override
public void removeItem(String item) {
items.remove(item);
}
@Override
public Collection getItems() {
return items;
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
}
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet CartServlet</title>");
out.println("</head>");
out.println("<body>");
final Context context= new InitialContext();
CartBeanRemote cart = (CartBeanRemote)context.lookup
("java:global/CartStatefulEJB/CartBean");
out.println("<br>Adding items to cart<br>");
cart.addItem("Pizza");
cart.addItem("Pasta");
cart.addItem("Noodles");
cart.addItem("Bread");
cart.addItem("Butter");
out.println("<br>Listing cart contents<br>");
Collection items = cart.getItems();
for (Iterator i = items.iterator(); i.hasNext();)
{
String item = (String) i.next();
out.println("<br>" + item);
}
}catch (Exception ex){
out.println("ERROR -->" + ex.getMessage());
}
out.println("</body>");
out.println("</html>");
out.close();
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
SUMMARY
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435
T.Y.I.T SEM-V ITCS CLASSES For BSc(I.T) & BSc(C.S) Enterprise Java
1ST FLOOR HINDUSTAN BLDG, T.P STREET, OPP. TO AKBAR PEERBHOY COLLEGE, M.S ROAD ABOVE
JAVED HOTEL GRANT ROAD(E) MUMBAI-03 Mob. 7977-079-435