Training

WLSKB008 - Developing and Testing EJB in WebLogic Workshop Tutorial

Popular Courses

Browse Our Free Resources

  • whitepapers
  • whitepapers
  • webinars
  • blogs

Our Locations

Training Centres

Vancouver, BC
Calgary, AB
Edmonton, AB
Toronto, ON
Ottawa, ON
Montreal, QC
Hunt Valley
Columbia

locations map

Calgary

550 6th Av SW
Suite 475
Calgary, AB
T2P 0S2

Toronto

821A Bloor Street West
Toronto, ON
M6G 1M1

Vancouver

409 Granville St
Suite 902
Vancouver, BC
V6C 1T2

U.S. Office

436 York Road
Suite 1
Jenkintown, PA
19046

Other Locations

Dallas, TX
Miami, FL

Web Age Solutions Inc.

Introduction

Attribute driven programming in Workshop makes it easy and quick to develop a session EJB. Developers need not deal with deployment descriptor files. Properties that are normally stored in ejb-jar.xml or WebLogic specific deployment descriptor weblogic-ejb-jar.xml, are stored in the EJB implementation class as XDoclet tags. The developer does not need to maintain the home or remote interface files either.

In this tutorial, you will develop a session bean that accesses database. You will unit test the EJB without developing a client code.

Create the EJB

Create a new application called BasicApp (unless you have already created this from an earlier tutorial).

Before we can add a new EJB, we must create a EJB project within the BasicApp application. From the menubar select, File->New->Project.

Select EJB and then EJB Project. Enter BasicAppEJB as the name of the project. Click on Create.

Right click on BasicAppEJB and select New->Folder. Enter ejbs as the folder name. Click on OK.

Right click on the ejbs folder and select New->Session bean. Enter CustomerManager.ejb as the name of the file. Click on Create. System will create the file and open it in the editor.

On the right hand side of the editor, under the Naming section, expand Remote EJB. Change the JNDI name to ejb/CustomerManager.

Right click on the diagram, and select Add Component Method. Set the method signature to java.util.ArrayList getCustomerList().

Click on the Source View tab of the editor. Add the following import statements.

import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;

Add a new member variable to the CustomerManager class as shown below.

private DataSource ds; 

Change the ejbCreate() method as follows.

public void ejbCreate() {
    try {
        Context ctx = new InitialContext();
 
        ds = (DataSource) ctx.lookup("cgDataSource");
    } catch (Exception e) {
        throw new EJBException("ejbCreate() failed", e);
    }
}

Note: The data source cgDataSource is automatically defined as a part of the Workshop installation.

Change the implementation of the getCustomerList() method to as shown below.

public java.util.ArrayList getCustomerList() 
{
    Connection con = null;
    ArrayList customerList = new ArrayList();
 
    try {
        con = ds.getConnection();
        PreparedStatement ps = con.prepareStatement(
            "select name from customer");
        ResultSet rs = ps.executeQuery();
 
        while (rs.next()) {
            customerList.add(rs.getString("name"));
        }
    } catch (Exception e) {
        throw new EJBException("getCustomerList failed", e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (Exception ignored) {
            }
        }
    }
 
    return customerList;
}

Save changes (Control+S). Right click on BasicAppEJB and select Build BasicAppEJB. (Note: You must build the project for the EJB to be deployed to the server).

Generate the EJB Control

A EJB control simplifies development of a EJB client. It hides the complexity of JNDI look up.

Under the BasicAppWeb project create a new folder called MyControls. Right click on MyControls and select New->Java Control. Choose EJB Control and enter CustomerManagerControl as the name of the control. Click on Next. Click on the Brows application EJBs button. Select the EJB as shown below.

Click on Select. Notice, how system automatically populates the JNDI name, home and remote interface properties of the control.

Click on the Create button. This will create the CustomerManagerControl.jcx file.

Unit Test the EJB

Once the EJB control is generated, we can unit test the EJB without developing a client. System can generate a web service that is used to test the EJB. Right click on CustomerManagerControl.jcx and select Generate Test JWS File (Stateless). This will create a new web service called CustomerManagerControlTest.jws.

Double click on the web services file to open it in the editor. From the menubar select Debug->Start.

Click on the getCustomerList button.

In the result screen, scroll down and locate the Service Response section. Make sure that you see all the customer names.

Use the EJB In a Page Flow

In the BasicAppWeb project create a basic Page Flow called ListCustomerFlow. Click on the Action View tab of the editor. Drag the CustomerManagerControl.jcx control from the MyControls folder on the Page Flow.

Click on the Source View tab of the editor. Locate the EJB control variable to see how the system adds the control to the Page Flow.

/**
 * @common:control
 */
private MyControls.CustomerManagerControl customerManagerControl;

Change the implementation of the begin action to as follows.

protected Forward begin() throws Exception
{
    getRequest().setAttribute(
        "customerList", customerManagerControl.getCustomerList());
 
    return new Forward("success");
}

Save changes (Control+S).

Open the index.jsp page. Set the contents of the <body> tag to as shown below.

<body>
 <h2>Customer Names</h2>
 
 <netui-data:repeater dataSource="{request.customerList}">
    <netui-data:repeaterItem>
        <netui:label value="{container.item}"/><br/>
    </netui-data:repeaterItem>
 </netui-data:repeater>
</body>

Save changes.

Test

Open a browser. Enter the URL: http://localhost:7001/BasicAppWeb/ListCustomerFlow/ListCustomerFlowController.jpf.

 


Feedback


Email Address: *


Very useful Somewhat useful Not bad Needs many corrections


Comments:
*


( * ) = mandatory field