EJB Component Lab Practice



EJB Component Lab Practice

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.

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 /bin and /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

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:

• Remote interface

• Home interface

• Enterprise bean class

//Remote interface: TempConv.java

package tempConv;

import javax.ejb.EJBObject;

import java.rmi.RemoteException;

public interface TempConv extends EJBObject {

public double fToC (double f ) throws RemoteException;

public double cToF (double c) throws RemoteException;

}

//Home interface: TempConvHome.java

package tempConv;

import java.io.Serializable;

import java.rmi.RemoteException;

import javax.ejb.CreateException;

import javax.ejb.EJBHome;

public interface TempConvHome extends EJBHome {

TempConv create() throws RemoteException, CreateException;

}

//Enterprise bean class: TempConvEJB.java

package tempConv;

import java.rmi.RemoteException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import java.math.*;

public class TempConvEJB implements SessionBean {

public double cToF(double c) {

return ((c*9.0/5.0 + 32)*100)/100.0;

}

public double fToC(double f) {

return ((f-32)*5.0/9.0*100)/100.0;

}

public TempConvEJB() {}

public void ejbCreate() {}

public void ejbRemove() {}

public void ejbActivate() {}

public void ejbPassivate() {}

public void setSessionContext(SessionContext sc) {}

}

//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", RMATION_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", RMATION_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

Temperature Converter

Temperature Converter

Enter a degree to convert:

0 ) {

double d = Double.parseDouble(degree);

%>

fahrenheit degrees are centigrade degrees.

centigrade degrees are fahrenheit degrees .

Step 4. Compiling all source code

1. On windows choose

Program | Sun Microsystem | J2EE 1.4 SDK | Start Default Domain

to start J2EE Application Server

Or start up J2EE Application Server by command line in a terminal window.

>asadmin start-domain --verbose domain1

2. Compile the source files at command prompt in C:\cop\ejb\tempconv directory. Type

>asant build

[pic]

A new directory build is created with class files.

Step 5. Deployment

1. Start deploytool

In Windows choose Program | Sun Microsystem | J2EE 1.4 SDK | deploytool

to start deploytool utility.

Or on command line type

>deploytool

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

3. 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

[pic]

• 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.

[pic]

● 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         

 

     

     

4. Packaging the Application Client

To start the New Application Client wizard, select File | New | Application Client

• In Jar File Contents dialog box select Create New AppClient Module in Application radio button.

• Select TempConvApp in the combo box

• Enter TempConvClient in the AppClient Name field.

• In the tree under Available Files, go to c:\cop\ejb\tempconv\build directory.

• Select the TempConvClient.class file and click Add.

• Click OK and Next.

• Select TempConvClient in the Main Class combo box in the General dialog box,

• Select container-managed authentication in the Callback Handler Class combo box,.

[pic]

• Now click Next and Finish.

Specifying the Application Client's Enterprise Bean Reference:

• In the tree, select TempConvClient.

• Select the EJB Refs tab, Click Add.

• Type ejb/ClientConv in the Coded Name column, select Session in the Type field, select Remote in the Interfaces field

• Type tempConv.TempConvHome in the Home Interface field, type tempConv.TempConv In the Local/Remote Interface field

[pic]

After the packaging process, you can view the deployment descriptor by selecting

Tools | Descriptor Viewer.

   TempConvClient    

      ejb/ClientConv       Session       tempConv.TempConvHome       tempConv.TempConv    

4. 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.

[pic]

• 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.

[pic]

Specifying the Web Client's Enterprise Bean Reference:

• In the tree, select TempConvWAR

• Select the EJB Refs tab

• Click Add

• In the Coded Name field, enter ejb/WebConv

• Select Session in the Type field

• Select Remote in the Interfaces field.

• Type tempConv.TempConvHome in the Home Interface field, type tempConv.TempConv In the Local/Remote Interface field

After the packaging process, you can view the following deployment descriptor by selecting ToolsDescriptor Viewer.

  

TempConvWAR    

      index1      

 index1       

/index1.jsp    

   

      ejb/WebConv      

 Session       

tempConv.TempConvHome       

tempConv.TempConv   

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.

[pic]

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.

[pic]

2. 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.

[pic]

[pic]

3. Running the Web Client

To run the Web client, point your browser at the following URL. Replace with the name of the host running the J2EE server. If your browser is running on the same host as the J2EE server, you may replace with localhost. By default your URL is .

You should see the screen shown in Figure after entering 100 in the input field and clicking .

[pic]

[pic]

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download