I ask this question and add code samples because of the pressing need to be clear about how i used to know how ejb applications are implemented in the past i.e 5 years earlier using sun one appserver 1.4 and the way it is done now using netbeans IDE (latest series with glassfish server). I know earlier that to implement an ejb application, there would be an HOME interface with a method giving a reference to a the REMOTE interface as return type. The REMOTE interface declares the business methods that gets implemented by an implementation class which serves as the ejb class. I've observed that recent implementations of ejb apps using netbeans seem to have left off the notion of HOME interface. My first question his:
(1) Why is there no longer a reference to the HOME interface? In short what has changed?
In a recent J2EE tutorial i came across, when it came to stage of packaging the ejb components, there was this mention of
packaging a Cart interface , CartHome interface and CartBean class. I looked through the material, i could not see where the
CartHome interface was implemented.
(2) My second question is even after i've implemented the ejb components correctly. How do i call the same correctly in a JSP
page?
The code samples i supply here were implemented in sun one appserver years back. Now i'm using it as practice material
to be acquainted better with ejb technologies and to know how i can implement it using netbeans IDE. At the moment my
netbeans IDE version is 7.4. Another thing is that i'm interested in the stateful session bean applications. There seem to
be no implementing of it even in netbeans ejb tutorials. The emphasis is mostly on stateless session beans...
HERE ARE THE CODE SAMPLES: CODE LINES THAT GAVE NO ISSUES A LEFT OUT, COMMENT LINES ARE SHOWN
STATEFUL SESSION BEAN HOME INTERFACE:
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;
}
STATEFUL SESSION BEAN REMOTE INTERFACE
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;
}
STATEFUL SESSION BEAN CLASS
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())
{
//the remaining logic goes on...
}
}
else
{
..........
}
}
public String removeCourse(String Cname)
{
//code logic goes here...
}
public Vector getSelectedCourse()
{
return selectedCourse;
}
public CourseBean() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void setSessionContext(SessionContext sc) {}
ALL THIS CODE I WOULD HAVE WANTED TO IMPLEMENT IN MY VERSION OF NETBEANS. BUT IN THE SPIRIT OF MY
OBSERVATIONS WITH A RECENT IDE LIKE NETBEANS, I ONLY IMPLEMENTED THE REMOTE INTERFACE AND THE
BEAN CLASS WITH NO IMPLEMENTING OF EJBObject and SessionBean. I BELIEVE THE AREA OF CONTENTION [/SIZE]IS IN THE CourseCart.jsp page where the ejb will be invoked after
a user has supplied the a student name and roll number in form text fields. I will supply only the JSP SCRIPLETS of the
page as implemented in page at the time...
initializing code...before the starting HTML tag
<%!
private Course course = null;
CourseHome home = null;
public void jspInit()
{
//to locate the stateful session bean
try
{
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("java:comp/env/ejb/Univ");
home =(CourseHome)PortableRemoteObject.narrow(objRef, CourseHome.class));
}
catch(Exception ex)
{}
}
%>
//Another area to note...inside the form tag of CourseCart.jsp
<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);
}
}
%>
I tried implementing this code using my version of netbeans but i kept receiving 500 internal
source code error giving me null pointer exception.
I'll appreciate anyone that can be of assistance in giving clear and concise answers to the
questions given earlier:
(1) How can i implement this stateful session bean app in recent NETBEAN IDE
(2) Why does recent NETBEAN IDE version have no automatic support for
EJBObject, SessionBean and Home interface. What has changed and why?
(3) What is the correct way to invoke a stateful session bean from a JSP page
especially a stateful session bean class that supplies implementation code
for a lifecycle method like ejbCreate() ?
Olakunle Oni