Temperature Converter Session Bean Component Development | SWE 4633, Lab Reports of Engineering

Material Type: Lab; Professor: Qian; Class: Comp-BasedSoftwareDevelopment; Subject: Software Engineering; University: Southern Polytechnic State University; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 08/03/2009

koofers-user-g43
koofers-user-g43 🇺🇸

9 documents

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EJB Component Lab Practice
1. Complete the following labs: lab4
2. Google a open source stateful shopping cart component and build your own
client to reuse the shopping cart
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.
4.5.1 Lab1: Temperature Converter Session Bean Component Development
Step1. Installation and configuration for lab1 and lab2
1. Download J2EE 1.4 (J2EE1.4 application server, J2SE1.4.x, J2EE1.4 samples) and install J2EESDK
1.4 with JDK 1.4.2 on your machine.
2. Add <J2EE_HOME>/bin and <JAVA_HOME>/bin to the front of the path environment variable so
that J2EE 1.4 Application Server scripts (asadmin, asant, deploytool,and wscompile)
override other installations
3. In order to run the asant scripts create directory structure, properties files and XML files as follows:
Create working directory c:\cop\ejb.
Under c:\cop\ejb directory create two sub-directories: common, tempconv
Under c:\cop\ejb\tempconv create two sub-directories: src and web. All the Java
source code files will be stored in the src sub-directory, and web related files like .jsp files
will be in web sub-directory.
Under c:\cop\ejb\common create two files: build.properties and
targets.xml. Under c:\cop\ejb\tempconv create build.xml file. Assume that
the J2EE 1.4 Application Server installation is C:\Sun\AppServer.
Or just simply copy the entire COP directory in Lab4 of the companion CD to c:\. It will
take care of all above configurations.
build.property is as the follows:
j2ee.home=C:/Sun/AppServer
sunone.home=${j2ee.home}
admin.password=
admin.host=localhost
admin.user=admin
admin.port=4848
https.port=1043
domain.resources="domain.resources"
domain.resources.port=8080
db.root=${j2ee.home}/pointbase
db.driver=com.pointbase.jdbc.jdbcUniversalDriver
db.host=localhost
db.port=9092
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Temperature Converter Session Bean Component Development | SWE 4633 and more Lab Reports Engineering in PDF only on Docsity!

EJB Component Lab Practice

1. Complete the following labs: lab

2. Google a open source stateful shopping cart component and build your own

client to reuse the shopping cart

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.

4.5.1 Lab1: Temperature Converter Session Bean Component Development

Step1. Installation and configuration for lab1 and lab

  1. Download J2EE 1.4 (J2EE1.4 application server, J2SE1.4.x, J2EE1.4 samples) and install J2EESDK 1.4 with JDK 1.4.2 on your machine.
  2. Add < J2EE_HOME >/bin and < JAVA_HOME >/bin to the front of the path environment variable so that J2EE 1.4 Application Server scripts (asadmin, asant, deploytool,and wscompile) override other installations
  3. In order to run the asant scripts create directory structure, properties files and XML files as follows:  Create working directory c:\cop\ejb.  Under c:\cop\ejb directory create two sub-directories: common, tempconv  Under c:\cop\ejb\tempconv create two sub-directories: src and web. All the Java source code files will be stored in the src sub-directory, and web related files like .jsp files will be in web sub-directory.  Under c:\cop\ejb\common create two files: build.properties and targets.xml. Under c:\cop\ejb\tempconv create build.xml file. Assume that the J2EE 1.4 Application Server installation is C:\Sun\AppServer.  Or just simply copy the entire COP directory in Lab4 of the companion CD to c:. It will take care of all above configurations. build.property is as the follows: j2ee.home=C:/Sun/AppServer sunone.home=${j2ee.home} admin.password= admin.host=localhost admin.user=admin admin.port= https.port= domain.resources="domain.resources" domain.resources.port= db.root=${j2ee.home}/pointbase db.driver=com.pointbase.jdbc.jdbcUniversalDriver db.host=localhost db.port=

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

  1. Packaging the EJB To package an enterprise bean, you run the New Enterprise Bean wizard of the deploytool utility which will create the bean’s deployment descriptor, package the deployment descriptor and the bean’s classes in an EJB JAR file, and Inserts the EJB JAR file into the application’s TempConvApp.ear file. To start the New Enterprise Bean wizard, select File | New | Enterprise Bean.  In the EJB JAR dialog box select the Create New JAR Module in Application radio button.  In the combo box, select TempConvApp.  In the JAR Display Name field, enter TempConvJAR.  Click Edit.  In the tree under Available Files, go to C:\cop\ejb\tempconv\build directory.  Select TempConv folder from the Available Files tree and click Add  Click OK. And Next

 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

  1. Packaging the Application Client

After the packaging process, you can view the deployment descriptor by selecting Tools | Descriptor Viewer.

TempConvClient ejb/ClientConv Session tempConv.TempConvHome tempConv.TempConv

  1. Packaging the Web Client  Creates a Web application deployment descriptor  Adds the component files to a WAR file  Adds the WAR file to the application's TempConvApp.ear file

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.

  1. Deploying the J2EE Application Now this J2EE application contains all components, it is ready for deployment.  Select the TempConvApp application.  Select Tools | Deploy.  Select the checkbox labeled Return Client Jar.  Verify the full path name for the file TempConvAppClient.jar so that it will reside in the c:\cop ejb\tempconv subdirectory. The TempConvAppClient.jar file has the EJB stub that enable remote access to TempConvEJB.  Click OK.
  1. Running the J2EE Application Client In a terminal window, go to the c:\cop\ejb\tempconv directory, check TempConvApp.ear and TempConvAppClient.jar files. > appclient -client TempConvAppClient.jar You will see the screen shown in the figure after entering 100 in the input field and clicking OK.
  2. Running the Web Client

4.5.2 Lab 2: A Complete J2EE Application with one Session Bean and two Entity Beans

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();