SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Stateful Session Bean - Complete Project

 Introduction

Stateful session beans are appropriate if any of the following conditions are true:

  • The bean’s state represents the interaction between the bean and a specific client.
  • The bean needs to hold information about the client across method invocations.
  • The bean mediates between the client and the other components of the application, presenting a simplified view to the client.
  • Behind the scenes, the bean manages the work flow of several enterprise beans.

Types of Enterprise Beans are
  • Session Beans
    1. Stateless Session Beans
    2.  Stateful Session Beans
  • Entity Beans
    1. Bean Managed Persistent (BMP) Entity Beans
    2. Container Managed Persistent (CMP) Entity Beans
  • Message-Driven Bean
CLICK HERE to download this complete example (zip file)


 Create the Stateful Session Bean Home Interface (CourseHome.java)

package University;

import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface CourseHome extends EJBHome
{
Course create(String StudentName, String RollNo) throws RemoteException, CreateException;
}
Download: CourseHome.java

 Create the Stateful Session Bean Remote Interface (Course.java)

package University;

import java.util.*;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Course extends EJBObject
{
public String addCourse(String Cname) throws RemoteException;
public String removeCourse(String Cname) throws RemoteException;
public Vector getSelectedCourse() throws RemoteException;
}
Download: Course.java

 Create the Stateful Session Bean Class (CourseBean.java)

package University;

import java.util.*;
import javax.ejb.*;
public class CourseBean implements SessionBean
{
String studentName;
String studentRollNo;
Vector selectedCourse;
public void ejbCreate(String StudentName, String RollNo) throws CreateException
{
if (StudentName == null)
{
throw new CreateException("Null person not allowed.");
}
else
{
studentName= StudentName;
}
selectedCourse = new Vector();
}
public String addCourse(String Cname)
{
if (Cname==null)
{
return "";
}
try
{
if(!selectedCourse.isEmpty())
{
Enumeration enumer = selectedCourse.elements();
String title="";
while(enumer.hasMoreElements())
{
title = (String)enumer.nextElement();
if(title!=null)
{
if(Cname.equals(title))
{
return "You have already selected this course";
}
}
}
selectedCourse.addElement(Cname);
}
else
{
selectedCourse.addElement(Cname);
}
}
catch(Exception e)
{
return "Error";
}
return "";
}
public String removeCourse(String Cname)
{
boolean result = selectedCourse.removeElement(Cname);
if (result == false)
{
return Cname + "course is not in cart.";
}
return "Course Removed";
}
public Vector getSelectedCourse()
{
return selectedCourse;
}
public CourseBean()
{}
public void ejbRemove()
{}
public void ejbActivate()
{}
public void ejbPassivate()
{}
public void setSessionContext(SessionContext sc)
{}
}
Download: CourseBean.java

 Create the Web Client (Index.jsp)

<html>
<body>
<h1><center>Student Information</center></h1>
<hr>
<p>Enter Student Name:</p>
<form method="get" name="f1" action="CourseCart.jsp">
<input type="text" name="stdname" size="25" />
<p>Enter Student Roll Number:</p>
<input type="text" name="stdrollno" size="25" />
<br>
<p>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</p>
</form>
</body>
</html>
Download: Index.jsp

 Create the Web Client (CourseCart.jsp)

<%@ page import="javax.ejb.*,javax.naming.*, javax.rmi.PortableRemoteObject,java.rmi.RemoteException, java.math.*, University.Course,University.CourseHome,java.util.*" %>
<%!
private Course course = null;
CourseHome home=null;
public void jspInit()
{
Try
{
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("java:comp/env/ejb/Univ");
home=(CourseHome)PortableRemoteObject.narrow(objRef, CourseHome.class);
}
catch(Exception ex)
{}
}
%>
<html>
<head>
<title>University Registration Application</title>
</head>
<body bgcolor="white">
<form method="get" name="f1">
<%
String stdname = request.getParameter("stdname");
String stdrollno = request.getParameter("stdrollno");
if ( stdname != null && stdname.length() > 0 )
{
if ( stdrollno != null && stdrollno.length() > 0 )
{
course = home.create(stdname,stdrollno);
}
}
%>
<H1>University Registration Application</h1>
<p>
Welcome <%=stdname%>
<p>
Please a Course Subject
<select name=ch size=1>
<option>Geography</option>
<option>History</option>
<option>Zoology</option>
<option>Botany</option>
</select>
<br>
<%
int i=1;
String choice=request.getParameter("ch");
if(choice!=null)
{
if(!choice.equals("--"))
{
String str=course.addCourse(choice);
%>
<p>
<%=str%>
</p>
<%
}
}
String rmcourse=request.getParameter("c");
if(rmcourse!=null)
{
course.removeCourse(rmcourse);
}
Vector courseList = new Vector();
courseList = course.getSelectedCourse();
Enumeration enumer = courseList.elements();
%>
<form method="get" name="f2">
<table border=1>
<tr>
<th>
Course Name
</th>
<th>
Select Course to remove
</th>
</tr>
<%
while (enumer.hasMoreElements())
{
String title = (String) enumer.nextElement();
if (title!=null)
{
%>
<tr>
<td>
<%=title%>
</td>
<td>
<input type=radio name=c value=<%=title%>>
</td>
</tr>
<%
}
}
%>
</table>
<input type="hidden" name="stdname" size="25" value=<%=stdname%>>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
Download: CourseCart.jsp


 Click Next To Continue ...
<- PREVIOUSNEXT ->