



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Professor: Qian; Class: Comp-BasedSoftwareDevelopment; Subject: Software Engineering; University: Southern Polytechnic State University; Term: Unknown 1989;
Typology: Lab Reports
1 / 43
This page cannot be seen from the preview
Don't miss anything!




































This section is designed to enhance the understanding of the EJB concepts by providing some concrete examples and step-by-step guidelines demonstrating how to build EJB components, how to assemble and deploy EJB components, and how to build different clients for these components and run the clients. Here are two examples for lab practice. Lab 1 describes the steps to build a session bean called temperature converter component and to deploy it by the utility tool deploytool in J2EE 1.4.x. The client of this temperature converter EJB application is either a JSP web component which is accessed by a browser or a Java application client. Lab 1 also demonstrates the running result of a web client and an application client of this EJB component. Lab 2 is an implementation of a student registration system with one enroll session EJB component and two CMP entity EJB components for student and course respectively. Lab 2 shows the connection between the session bean and the entity beans, and association relationship between two entity beans.
Step1. Installation and configuration for lab1 and lab
db.sid=sun-appserv-samples db.url=jdbc:pointbase:server://${db.host}:${db.port}/${db.sid} url.prop=DatabaseName ds.class=com.pointbase.jdbc.jdbcDataSource build=build conpool.name=PointBasePool jdbc.resource.name=jdbc/PointBase targets.xml is shown here:
build.xml is shown here: ]>
&targets;
Step 2. Coding the Enterprise Bean The enterprise bean in this example includes the following code:
//Application Client for EJB import tempConv.TempConvHome; import tempConv.TempConv; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import java.text.DecimalFormat; import javax.swing.JOptionPane; public class TempConvClient { public static void main(String[] args) { String output = ""; String sDegree = ""; double degree; DecimalFormat twoDigits = new DecimalFormat ("0.00"); try { Context initial = new InitialContext(); Context myEnv = (Context)initial.lookup("java:comp/env"); Object objref = myEnv.lookup("ejb/ClientConv"); TempConvHome home = (TempConvHome)PortableRemoteObject.narrow(objref, TempConvHome.class); TempConv converter = home.create(); sDegree = JOptionPane.showInputDialog("Enter centigrade degree"); degree = Double.parseDouble( sDegree); output = sDegree + " centigrade degrees are " + twoDigits.format (converter.cToF(degree)) + " fahrenheit degrees " ; JOptionPane.showMessageDialog ( null, output, "results", JOptionPane.INFORMATION_MESSAGE); sDegree = JOptionPane.showInputDialog ( "Enter fahrenheit degree" ); degree = Double.parseDouble( sDegree); output = sDegree + " fahrenheit degrees are " + twoDigits.format(converter.fToC(degree)) + " centigrade degrees" ; JOptionPane.showMessageDialog ( null, output, "results", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } catch (Exception ex) { System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); } } } Step 3. Development of application client and web client for EJB component // Web Client for the EJB: Index.jsp <%@ page import="tempConv.TempConv,tempConv.TempConvHome,javax.ejb., java.math., javax.naming.*,
javax.rmi.PortableRemoteObject, java.rmi.RemoteException, java.text.DecimalFormat" %> <%! private TempConv conv = null; public void jspInit() { try { InitialContext ic = new InitialContext(); Object objRef = ic.lookup("java:comp/env/ejb/WebConv"); TempConvHome home = (TempConvHome)PortableRemoteObject.narrow(objRef, TempConvHome.class); conv = home.create(); } catch (RemoteException ex) { System.out.println("Couldn't create TempConv bean."+ ex.getMessage()); } catch (CreateException ex) { System.out.println("Couldn't create TempConv bean."+ ex.getMessage()); } catch (NamingException ex) { System.out.println("Unable to lookup home: "+ "WebConv "+ ex.getMessage()); } } public void jspDestroy() { conv = null; } %>
Temperature Converter
Temperature Converter
Enter a degree to convert:
<% DecimalFormat twoDigits = new DecimalFormat ("0.00"); String degree = request.getParameter("degree"); if ( degree != null && degree.length() > 0 ) { double d = Double.parseDouble(degree); %> <% if (request.getParameter("fToC") != null ) { %>
<%= degree %> fahrenheit degrees are <%= twoDigits.format (conv.fToC(d)) %> centigrade degrees.
## 2. Creating the J2EE Application In deploytool, select File | New | Application. Click Browse. In the file chooser, navigate to c:\cop\ejb\tempconv. In the File Name field, enter TempConvApp.ear. Click New Application and OK
In the General dialog box select tempCovn.TempConvEJB in the Enterprise Bean Class combo box. Verify TempConvEJB in the Enterprise Bean Name field. Under Bean Type, select the Session and Stateless radio buttons. Select tempConv.TempConvHome in the Remote Home Interface combo box, select tempConv.TempConv in the Remote Interface combo box, Click Next. ● Click Finish in Transaction Management dialog box After the packaging process, you can view the deployment descriptor by selecting Tools | Descriptor Viewer.
TempConvJAR
TempConvEJB tempConv.TempConvHome tempConv.TempConv tempConv.TempConvEJB Stateless Bean
After the packaging process, you can view the deployment descriptor by selecting Tools | Descriptor Viewer.
TempConvClient ejb/ClientConv Session tempConv.TempConvHome tempConv.TempConv
Build the web component by the New Web Component wizard, select File | New | Web Component. See the following dialog boxes. In the WAR File dialog box select Create New WAR File in Application. Select TempConvApp in the combo box Enter TempConvWAR In the WAR Name field,. Click Edit. In the tree under Available Files, go to C:\cop\ejb\tempconv\web directory. Select index.jsp and click Add. Click OK. and Next. In the Choose ComponentType dialog box select JSP radio button. Click Next In the Component General Properties dialog box select index.jsp in the JSP FileName combo box Click Finish.
Specifying the JNDI Names Follow the following steps to map the enterprise bean references used by the clients to the JNDI name of the bean, In the tree, select TempConvApp. Select the JNDI Names tab. To specify a JNDI name for the bean, find the TempConvEJB component in the Application table and enter MyTempConv in the JNDI Name column. To map the references, in the References table enter MyTempConv in the JNDI Name for each row. Click Web Context tab, enter tempconv in the Contest Root field, so tempconv becomes the context root in URL.
The lab2 implements a student registration application “EnrollerApp” which consists of a front session bean and two backend CMP entity beans supported by database tables. It can add new course or new student into the registration system, remove a course, or remove a student. It can also register a student with a number of courses or drop a student from one or many courses registered. The system can also report some statistical data of registration such as the enrollment of a specific course. This EnrollerApp application has four components. The EnrollerAppClient component is a J2EE application client that accesses the EnrollerEJB session bean through the bean’s remote interface. The EnrollerBean accesses two entity beans — StudentBean and CourseBean — through their local interfaces. The entity beans use container-managed persistence and relationships. The StudentBean and CourseBean entity beans have many-to-many relationship. Each bean has a relationship field whose value identifies the related bean instance. The diagram below shows the components and their relationships of the EnrollerApp application. The dotted lines represent the access gained through invocations of the JNDI lookup method. The solid lines represent the container- managed relationships.
The example in this lab will be tested with a PointBase database, which provides persistent storage for application data. The PointBase is included in the J2EE SDK. Step 1. Develop Beans and their client Student entity bean component includes the following code: Local interface: LocalStudent .java Local home interface: LocalStudentHome.java Entity bean class: StudentBean.java Utility class: StudentDetails.java Assume that all the Java source files are placed in a working directory such as c:\cop\ejb\enroller. //LocalStudent.java is a Local interface for student CMP entity bean, // It can be a remote interface if its client is remote. package student; import java.util.; import javax.ejb.; public interface LocalStudent extends EJBLocalObject { public String getStudentId(); public String getName(); public Collection getCourses(); } //LocalStudentHome.java is a Home interface of student CMP entity bean //It can be defined as a remoteHome interface if accessed by a remote // client Student entity bean (CMP) Course entity bean (CMP)
Enroller session bean Client
context = ctx; } public void unsetEntityContext() { context = null; } public void ejbRemove() {} public void ejbLoad() {} public void ejbStore() {} public void ejbPassivate() { } public void ejbActivate() { } } // StudentBean class //StudentDetails.java package student; public class StudentDetails implements java.io.Serializable { private String id; private String name; public StudentDetails (String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } public String toString() { String s = "Student Id: " + id + " Name: " + name + "\n"; return s; } } // StudentDetails /*Course entity bean component includes the following code Entity bean class: CourseBean.java.java Local home interface: LocalCourseHome.java Local interface: LocalCourse.java Utility class: CourseDetails.java */ //LocalCourse.java
package course; import java.util.; import javax.ejb.; import student.; // Local interface of course CMP entity bean public interface LocalCourse extends EJBLocalObject { public String getCourseId(); public String getTitle(); public int getMaxEnrollment(); public int getCurrentEnrollment(); public Collection getStudents(); public ArrayList getStudentList(); public void addStudent(LocalStudent Student); public void dropStudent(LocalStudent Student); } //LocalCourseHome.java package course; import java.util.; import javax.ejb.; // Home interface of course CMP entity bean public interface LocalCourseHome extends EJBLocalHome { public LocalCourse create (String id, String title, int MaxEnrollment, int CurrentEnrollment) throws CreateException; public LocalCourse findByPrimaryKey (String id) throws FinderException; } //CourseBean.java package course; import java.util.; import javax.ejb.; import javax.naming.; import student.*; public abstract class CourseBean implements EntityBean { private EntityContext context; // Access methods for persistent fields: courseId, title, //maxEnrollment, currentEnrollment public abstract String getCourseId(); public abstract void setCourseId(String id); public abstract String getTitle();