0% found this document useful (0 votes)
19 views7 pages

Roorkee Institute of Technology: Ejb Introduction Mca 1St Year Rit

The document provides an introduction to Enterprise Java Beans (EJB), detailing its role as a specification for server-side components in Java that facilitate the development of secure and scalable applications. It outlines the types of enterprise beans, including session beans, entity beans, and message-driven beans, along with their functionalities and lifecycle management. Additionally, the document includes examples of stateless and stateful session beans, illustrating their implementation in Java code.

Uploaded by

vc63797
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Roorkee Institute of Technology: Ejb Introduction Mca 1St Year Rit

The document provides an introduction to Enterprise Java Beans (EJB), detailing its role as a specification for server-side components in Java that facilitate the development of secure and scalable applications. It outlines the types of enterprise beans, including session beans, entity beans, and message-driven beans, along with their functionalities and lifecycle management. Additionally, the document includes examples of stateless and stateful session beans, illustrating their implementation in Java code.

Uploaded by

vc63797
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EJB INTRODUCTION MCA 1ST YEAR RIT

ROORKEE INSTITUTE OF TECHNOLOGY


OBJECT ORIENTED PROGRAMMING WITH JAVA (CAT-011)

1. Introduction

The Enterprise Java Beans (EJB) is a specification for deployable server-side components in
Java. It is an agreement between components and application servers that enable any component
to run in any application server. EJB components (called enterprise beans) are deployable, and
can be imported and loaded into an application server, which hosts those components to
develop secured, robust and scalable distributed applications.

To run EJB application, you need an application server (EJB Container) such as Jboss, Glassfish,
Weblogic, Websphere etc. It performs:

1. Life Cycle Management


2. Security
3. Transaction Management
4. Load Balancing
5. Persistence Mechanism
6. Exception Handling
7. Object Pooling

EJB application is deployed on the server, so it is called server side component also.

2. Types of Enterprise Java Beans

EJB defines three different kinds of enterprise beans.

1. Session beans: Session bean contains business logic that can be invoked by local, remote or
webservice client. The bean class typically contains business-process related logic, such as
logic to compute prices, transfer funds between bank accounts, or perform order entry.Session
bean are of 3 types:

1. Stateless Session Bean: A stateless session bean doesn’t maintain state of a client
between multiple method calls. When a client invokes the methods of a stateless bean,
the bean’s instance variables may contain a state specific to that client but only for the
duration of the invocation. When the method is finished, the client-specific state should
not be retained.
2.Stateful Session Bean: A stateful session bean maintain state of a client across multiple
requests. In a stateful session bean, the instance variables represent the state of a unique
client/bean session. This state is often called the conversational state as the client interacts
with its bean.
EJB INTRODUCTION MCA 1ST YEAR RIT

3.Singelton Session Bean: A singleton session bean is instantiated once per application
and exists for the lifecycle of the application. Singleton session beans are designed for
circumstances in which a single enterprise bean instance is shared across and
concurrently accessed by clients.
2. Entity beans: Entity beans encapsulates the state that can be persisted in the database. User
data can be saved to database via entity beans and later on can be retrieved from the database
in the entity bean. The bean class contains data-related logic such as logic to reduce balance
of bank account or modify customer details.
3. Message-driven beans: Message-driven beans are similar to session beans in their actions. It
contains the business logic but it is invoked by passing message. The difference is that you
can call message driven beans only by sending messages to those beans. These message-
driven beans might call other enterprise beans as well.

Example1:- index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="abc" >
first number<input type="number" name="t1"><br>
second number<input type="number" name="t2"><br>
<input type="submit" value="submit">
</form>
</body>
</html>

Stateless session bean


package RIT;
import javax.ejb.Stateless;
@Stateless
EJB INTRODUCTION MCA 1ST YEAR RIT

public class addbean


{
private int i,j,k;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public int getK() {
return k;
}
public void add()
{
k=i+j;
}
}

Servlet test.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
EJB INTRODUCTION MCA 1ST YEAR RIT

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import RIT.addbean;
import javax.ejb.EJB;
public class abc extends HttpServlet
{
@EJB
addbean ob;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
int a=Integer.parseInt(request.getParameter("t1"));
int b=Integer.parseInt(request.getParameter("t2"));
ob.setI(a);
ob.setJ(b);
ob.add();
out.print(ob.getK());
}

Example 2:-
Remote Interface
package ejb;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface listelementsRemote
EJB INTRODUCTION MCA 1ST YEAR RIT

{
void adddElements(int a);
void removeElements(int a);
List<Integer> getElements();
}

Stateful session bean


package ejb;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
@Stateful
public class listelements implements listelementsRemote
{
List<Integer> values=new ArrayList<>();
@Override
public void adddElements(int a) {
values.add(a);
}
@Override
public void removeElements(int a) {
values.remove(new Integer(a));
}
@Override
public List<Integer> getElements() {
return values;
}
}
EJB INTRODUCTION MCA 1ST YEAR RIT

Index.jsp
<%@page import="java.util.List"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="ejb.listelementsRemote"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!
private static listelementsRemote values;
public void jspInit()
{try
{
InitialContext c=new InitialContext();
values=(listelementsRemote)c.lookup("java:global/ejbstateful/listelements");
}
catch(Exception e)
{
System.out.println(e);
}}
%>
<% if(request.getParameter("addbutton")!=null)
{
int a=Integer.parseInt(request.getParameter("t1"));
values.adddElements(a);
}
if(request.getParameter("delbutton")!=null)
{
EJB INTRODUCTION MCA 1ST YEAR RIT

int a=Integer.parseInt(request.getParameter("t1"));
values.removeElements(a);
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="index.jsp" method="POST">
<input type="text" name="t1" value="" /><br>
<input type="submit" value="add" name="addbutton"><br>
<input type="submit" value="delete" name="delbutton">
<%
if (values!=null)
{
List<Integer> nums=values.getElements();
for(int value: nums)
{
out.println("<br>"+value);
}
out.println("<br>"+nums.size());
}%>
</form>
</body>
</html>

You might also like