Enterprise Java Bean

Implementation of a Bean

1. Create the stateless session bean home interface.

2. Create the stateless session bean remote interface.

3. Create the stateless session bean class.

4. Create the Web client HTML file.

5. Create the Web client servlet.

6. Package the stateless session bean.

7. Package the Web client.

8. Specify Web client’s enterprise bean reference.

9. Deploy the application.

10. Test the application.


1: Create the Stateless Session Bean Home Interface

import javax.ejb.EJBHome;

public interface LoanHome extends EJBHome

{

public Loan create() throws java.rmi.RemoteException,

javax.ejb.CreateException;

}

2: Create the Stateless Session Bean Remote Interface

import javax.ejb.EJBObject;

public interface Loan extends EJBObject

{

public float calculateInterest(float rate, float time, float

amount) throws java.rmi.RemoteException;

}

3: Create the Stateless Session Bean Class

import java.util.*;

import java.io.*;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

public class LoanBean implements SessionBean {

private javax.ejb.SessionContext m_ctx = null;

public void setSessionContext(SessionContext ctx) {

m_ctx = ctx;

}

public void ejbCreate() throws java.rmi.RemoteException,

javax.ejb.CreateException {

System.out.println("ejbCreate() on obj " + this);

}

public void ejbRemove() {

System.out.println("ejbRemove() on obj " + this);

}

public void ejbActivate() {

System.out.println("ejbActivate() on obj " + this);

}

public void ejbPassivate() {

System.out.println("ejbPassivate() on obj " + this);

}

public float calculateInterest(float rate, float time, float

amount) throws java.rmi.RemoteException {

float interest = time * amount * (rate / 100);

return interest;

}

}

4: Create the Web Client HTML File

Calculate Interest

  

"http://127.0.0.1:8080/loanctx/servlet/LoanServlet">

RATE
TIME
AMOUNT "amount">


5: Create the Web Client Servlet

import javelin.*;

import javax.servlet.*;

import javax.naming.*;

import javax.servlet.http.*;

import javax.rmi.PortableRemoteObject;

import javax.ejb.*;

public class LoanServlet extends HttpServlet

{

public void doGet (HttpServletRequest

request,HttpServletResponse response)

throws ServletException, IOException

{

PrintWriter out = response.getWriter();

response.setContentType("text/html");

float rate =

Float.valueOf(request.getParameter("rate")).floatValue();

float time =

Float.valueOf(request.getParameter("time")).floatValue();

float amount =

Float.valueOf(request.getParameter("amount")).floatValue();

Loan myLoanRemote = null;

LoanHome myLoanHome = null;

InitialContext initContext = null;

try {

initContext = new InitialContext();

}catch (Exception e) {

out.println("First " + e.toString());

}

try {

String JNDIName = "ejb/SimpleLoan";

Object obj = initContext.lookup(JNDIName);

myLoanHome = (LoanHome)PortableRemoteObject.narrow(obj,

LoanHome.class);

}

catch(Exception e) {

out.println("Second " + e.toString()); }

try {

myLoanRemote = myLoanHome.create();

}catch(CreateException e) {

out.println("Third " + e.toString());

}

float interest = myLoanRemote.calculateInterest(rate, time,

amount);

out.println("Interest : " + interest + " ");

}

}

6: Package the Stateless Session Bean

The stateless session bean class files are packaged together in a single JAR file using the deploytool utility of the J2EE 1.4 Application Server. Before packaging the stateless session bean class files, a new application is created as an EAR file, to store the JAR and WAR files of the banking application. To create a new application, perform the following steps:


1. Select the FileÆNewÆApplication commandin the J2EE Deploytool

window to display the New Application dialog box, as shown in the following figure:

New Application Dialog Box


2. Click the Browse button in the New Application dialog box to display

another New Application dialog box. Select the directory where you want to

save the .ear file from the Look In combo box. Enter the name of the .ear file

as LoanApp in the File Name textbox, as shown in the following figure:

New Application Dialog Box


3. Click the New Application button to return to the New Application dialog

box, which contains the selected information, as shown in the following figure:


New Application Dialog Box


4. Click the OK button to create the new banking application, LoanApp.

The stateless session bean class files are packaged in a JAR file using the New

Enterprise Bean Wizard of the deploytool utility. To package the stateless session bean class files, perform the following steps:


1. Select the banking application, LoanApp,and select the

FileÆNewÆEnterprise Bean command in the J2EE Deploytool window to

open the New Enterprise Bean Wizard - Introduction dialog box, as

shown in the following figure:

New Enterprise Bean Wizard - Introduction Dialog Box


2. Click the Next button to open New Enterprise Bean Wizard – EJB JAR dialog box.


3. In New Enterprise Bean Wizard – EJB JAR dialog box, select the Create

New JAR Module in Application radio button from the JAR Location pane

and select the application, LoanApp.ear, from the drop-down box. Type the

name of the JAR file as loanJAR in the JAR Name text field, as shown in the

following figure:

New Enterprise Bean Wizard – EJB JAR Dialog Box


4. Click the Edit button to open the Edit Contents of loanJAR dialog box.

Select the Loan.class, LoanBean.class, and LoanHome.class files and click

the Add button to add these files. This will display the selected files in the

Contents of loanJAR pane of the Edit Contents of loanJAR dialog box, as

shown in the following figure:

Edit Contents of loanJAR Dialog Box


5. Click the OK button to return to the New Enterprise Bean Wizard – EJB

JAR dialog box, which now displays the stateless session bean class files, as

shown in the following figure:

New Enterprise Bean Wizard – EJB JAR Dialog Box


6. Click the Next button to open New Enterprise Bean Wizard – General

dialog box. Select the stateless session bean class, LoanBean, from the

Enterprise Bean Class drop-down box, stateless session bean remote home

interface, LoanHome, from the Remote Home Interface drop-down box

and stateless session bean remote interface, Loan, from the Remote

Interface drop-down box, as shown in the following figure:

New Enterprise Bean Wizard - General Dialog Box


7. Click the Next button to open New Enterprise Bean Wizard –

Configuration Options dialog box. Select the No radio button option from

the Expose Bean as Web Service Endpoint pane, as shown in the following figure:

New Enterprise Bean Wizard –Configuration Options Dialog Box


8. Click the Next button to open New Enterprise Bean Wizard – Next Steps

dialog box, as shown in the following figure:

New Enterprise Bean Wizard – Next Steps Dialog Box


9. Click the Finish button to close the New Enterprise Bean Wizard – Next

Steps dialog box. All the stateless session bean files are now packaged

together in a single JAR file and stored in the LoanApp banking application.


7: Package the Web Client

The Loan HTML file and LoanServlet servlet are combined and packaged into a single

WAR file. To package the HTML file and servlet, perform the following steps:


1. Select the banking application, LoanApp, and select the FileÆNewÆWeb

Component command to open the New Web Application Wizard-Introduction dialog box.


2. Click the Next button to open New Web Application Wizard - WAR File dialog box.


3. In New Web Application Wizard - WAR File dialog box, select the Create

New WAR Module in Application radio button option from the WARLocation pane and select the application, LoanApp, from the drop-down box.


Enter the name of the WAR file as loanWAR in the WAR Name text field, as

shown in the following figure:

New Web Application Wizard - WAR File Dialog Box


4. Click the Edit button to open the Edit Contents of loanWAR dialog box.

Select the Loan.html and LoanServlet.class files and click the Add button

to add these files. This will display the selected files in the Contents of

loanWar section pane in the Edit Contents of loanWAR dialog box, as shown in the following figure:


Edit Contents of loanWAR Dialog Box


5. Click the OK button to return to the New Web Application Wizard - WAR

File dialog box, which now displays the HTML file, Loan.html, as shown in the following figure:


New Web Application Wizard- WAR File Dialog Box


6. Click the Next button to open New Web Application Wizard - Choose

Component Type dialog box. Select the Servlet option, as shown in the

following figure:

New Web Application Wizard - Choose Component Type Dialog Box


7. Click the Next button to open the New Web Application Wizard -

Component General Properties dialog box. Select the LoanServlet servlet

file from the Servlet Class drop-down box. The Web Component Name and

the Web Component Display Name fields displays the corresponding

values, as shown in the following figure:

New Web Application Wizard - Component General Properties Dialog Box


8. Click the Next button to open the New Web Application Wizard - Next

Steps dialog box, as shown in the following figure:

New Web Application Wizard – Next Steps Dialog Box


9. Click the Finish button to close the New Web Application Wizard - Next

Steps dialog box. The HTML file and the servlet are packaged together in a

single WAR file, loanWAR, and added to the banking application, LoanApp EAR file.


10. Specify /loanctx as the Web context root for the Web application.

11. Select the loanWAR file, in the left pane and select the File Refs tab in the

right pane. Click the AddFile button on the Welcome Files pane and select

the Loan.html file from the drop down box in the Welcome Files pane.


8: Specify Web Client’s Enterprise Bean Reference

The Web client LoanServlet servlet refers the stateless session bean using JNDI name.

You need to set the enterprise bean’s JNDI name so that the servlet can refer the

stateless session bean for calculating loan interest. Step to set JNDI name is:


1. Select the banking application, LoanApp in deploytool’s left pane and select

the JNDI Names tab in the right pane. Specify ejb/SimpleLoan as the JNDI Name in the Application pane, as shown in the following figure:


J2EE deploytool: LoanApp (changed) Window


9: Deploy the Application

After you have packaged the stateless session bean files and the Web client, you need

to deploy the banking application on to the J2EE Application Server. The steps to

deploy the banking application, LoanApp on to the J2EE Application Server using the

deploytool utility are:


1. Select the banking application, LoanApp, in the left pane of the J2EE

Deploytool window.


2. Select the ToolsÆDeploy command to open the Deploy Module dialog box.

Ensure that localhost:4848 option is selected in the Server drop-down box.

Type the J2EE application server user name and password in the User Name

and Password text field, as shown in the following figure:

Deploy Module LoanApp Dialog Box


3. Click the OK button in the Deploy Module LoanApp dialog box to open the

Distribute Module dialog box that shows the progress of the deployment

process. When the deployment is complete, the Distribute Module dialog box

shows the successful deployment of the banking application, LoanApp, as

shown in the following figure:

Distribute Module Dialog Box


4. Click the Close button to close the Distribute Module dialog box and return

to the J2EE Deploytool window.


10: Test the Application

After the stateless session bean application is packaged and deployed to the J2EE

Application Server, you can access services of this application from a client browser.

To access the application from a Web browser, open a Web browser installed on your computer.


Type http://localhost: 8080/loanctx in the address bar of the Web browser to open the Loan HTML page in the Web browser. You can enter the relevant data in the different fields of the Loan HTML page to calculate loan interest.


The following figure shows the Loan HTML page in Internet Explorer:

Loan HTML Page


Click the Calculate button on the LoanHTML page to display the loan interest. The HTML page calls the servlet, LoanServlet, to calculate the loan interest amount. The

following figure shows the output of the servlet, LoanServlet:

Output of the LoanServlet Servlet












1 comments:

App Development Company India said...

It was very useful for me. Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us.

Post a Comment