Pramati Technologies

Writing EJB 2.0 Application on Pramati Studio

Overview

Writing and assembling enterprise beans become complex while modelling real-world business functions closely. New features in Pramati Studio 3.5 have made it easier to write the EJB 2.0 family of beans.

A Container Managed Persistence (CMP) model is at the core of EJB 2.0 and depends on Container Managed Relationships (CMR). This sample describes how to construct two entity beans, provide relationship, and deploy the beans on the in-built Pramati Server. A simple JSP acts as the client that looks up the beans. The sample can be developed and run using other development servers. For instructions, read the chapters on working with other servers in Studio. The sample is a small banking application that takes a Customer ID as input and generates the detailed account information for that customer.

This document describes how to create a CMP 2.0 application named PiggyBank. In the instructions, we assume that the user is working on Windows platform and Studio is installed in the directory c:\PStudio35_2026 on Windows.

Creating a desk

Desk is necessary to organize application sources. To create a desk, select Desk > New from the main menu.

  1. Enter Desk Name as PiggyBank.
  2. The location is selected as the Studio installation directory by default.
Note: Every desk maps to an underlying directory structure in the file system. Creating a new desk creates a corresponding directory with the same name in the file system.
  1. The template for the desk is selected as Default, automatically. This creates the source roots for EJB and Web module as c:\PStudio35_2026\PiggyBank\PiggyBankEJB and c:\PStudio35_2026\PiggyBank\PiggyBankWeb. The directory c:\PStudio35_2026\PiggyBank\classes stores the compiled classes.
Note: Every desk is based on a template provided by Studio. The default template for the desk contains - EJB 2.0 module, Web module, JAR, WAR, RAR and EAR.
  1. Click OK.

The desk is created and displayed in the explore panel. Read `Quick tour of Pramati Studio' to understand Studio UI.

Preparing a data resource

To prepare a datasource, it is necessary to add a JDBC driver, create tables on the target database, and define data resource on a server as explained in the following sections.

Adding JDBC drivers

To add JDBC drivers in Studio, select Tools > Configure JDBC Drivers from the main menu. The Configure Drivers panel, enables adding new drivers, modifying or deleting drivers. Cloudscape driver is configured in Studio, by default and other drivers can be configured. For example, to add Oracle driver:

    1. Click Add.
  1. In the add Driver panel, enter the name of the driver as Oracle.
  2. Click plus in the Classpath field, and browse for the driver.zip file for example, classes111.zip or classes12_01.zip. Adding the classpath populates the Driver Class Name as [NonXA]oracle.jdbc.driver.OracleDriver, automatically.
  3. Click OK.
Note: If the Cloudscape server is already running, a warning with the message "Server is already running, any resource with a new driver will not be added. To do the above, the server must be restarted" appears.

Creating tables on target database

PiggyBank application requires customer and account tables. Studio ships with the following database script files that create tables:

To use another database, you may study these scripts and modify or write a suitable script. To execute the scripts and prepare the tables for the application, choose your database and follow these steps:

Oracle

  1. Open SQL*PLUS editor.
  2. Enter the username and password to access the database. Oracle provides username scott and password tiger for its database product.
  3. From the SQL prompt, run:
@c:/pramati_studio/samples/oracle_ejb.sql

IBM Informix

  1. Start the SQL Editor.
  2. Connect to the database and run:
@c:\pramati_studio\samples\informix_ejb.sql

MS SQL Server

  1. Create the database protondemo using SQL Server Enterprise Manager.
  2. Open Query Analyser on SQL Server 7.0 or iSQL on SQL Server 6.5.
  3. Select protondemo as database and run:
@c:\pramati_studio\samples\mssql_ejb.sql

Defining data resource on a server

For entity beans, data resources should be defined on the application server they are being deployed on. Resources can be de fined on the in- built server to obtain connections. On Studio, resources are defined using the Resource Tool and are always local to the desk.

To start Resource Tool, select Tools > Resources. Define a resource connection under Data Source tab. Ensure that Cloudscape is running before adding the resource to the database - Informix Cloudscape. To do this on a Windows platform, run cloudscape.bat available under <install_dir>. On Unix platform run cloudscape.sh available under <install_dir>.

Note: A sample Cloudscape database with predefined tables is shipped with Studio, on which the application can be tested.

For Informix Cloudscape:

    1. Enter custres as the JNDI Name of the resource.
  1. Select com.cloudscape.core.RmiJdbcDriver as the Driver.
  2. Select the URL of the database as [Non-XA] cloudscape jdbc:rmi:jdbc:cloudscape:samples/sample_db from the list in the combo box.
  3. Select Container as the authorization mode.
  4. Click Test to verify the resource connection. If the connection is valid, click Add to add the resource on the server.

If the database used is Oracle, follow the given steps.

  1. Create the required tables using the scripts.
  2. Enter custres as the JNDI Name of the resource.
  3. Select [Non-XA] Oracle oracle.jdbc.driver.OracleDriver as the Driver.
  4. Select the correct URL of the database from the dropdown. For example, jdbc:oracle:thin:@127.0.0.1:1521:ORCL.
  5. Select Container as the authorization mode.
  6. Use default username scott and password tiger to access Oracle database.
Note: The username and password is user-defined and is required to access the database tables.
  1. Click on Test to verify the resource connection. If the connection is valid, click on Add to add the resource on the server.
Note: Clicking directly on Add will test and, if successful, add the data resource to the server. If you are modifying a resource, click on Modify after making the changes.
Generating Account bean with CMP 2.0

To create a CMP 2.0 bean, first start the Bean Wizard from the main menu by selecting Tools > Bean Wizard.

Selecting bean type

    1. Select PiggyBankEJB as the module.
  1. Select Entity- Container Managed 2.0 as the bean type.
  2. The option Use Database is checked by default. Retain this setting as this bean is required to use the sample database to build a relationship with another bean.
  3. Enter com.sample.cmr.Account as the bean class name. The name should be fully qualified. This automatically names the EJB as Account with JNDI name as Account_PiggyBankEJB_PiggyBank
  4. Leave the option Expert mode unchecked, and click Next.
Note: Expert mode takes user on an exhaustive path to write a customized bean. Quick mode takes the user on a simpler path to create a bean with standard interfaces and does not include creating ejbHome, ejbSelect, and the business method skeletons. Interfaces with default values are created. It also does not include steps to publish methods in Home, Local Home, Remote and Local interfaces.

Creating Container Managed Fields

Container Managed Fields (CMFs) of the entity bean can be created using an existing table in the database.

    1. Click Next in the bean wizard. Select custres as the name of the resource in the panel. Check the status of the driver by clicking Test or click Add to add datasource without testing.
  1. Enter the Table Filter as ACC* and click Get. This filters in all tables starting with letters ACC and displays them in the Tables field.
Note: The filter is case sensitive.
  1. Account is selected as the table, by default. The table details such as field name, type of field, primary key and primary key defined in the bean are displayed.
  2. The field CMP Fields for Customerid is unchecked, by default. Click Next. Define Container Managed Relations panel appears.
  3. Click Next as CMRs are not required for Account bean.

Defining create methods

  1. Move all CMF fields into Fields for Create method field using the right arrow.
  2. Clicking plus to generate ejbCreate method with CMF fields as the parameters.
  3. Click Next.

Defining finder methods

Finder methods are not required for the Account bean. Click Finish.

Components generated for PiggyBank

The wizard creates the bean Account, the bean class file, its remote and local interfaces for PiggyBank. It stores all the files under the EJB module, PiggyBankEJB on the desk, PiggyBank.

Generating a bean with CMR

The second bean to be generated for the application is Customer. It has a CMR with Account bean that was created earlier.

To create a new bean under a module, right click on the EJB module PiggyBankEJB and select New File. Select EJB and click OK. This starts the Bean Wizard.

Select bean type

The module is selected as PiggyBankEJB.

  1. Select Entity Container Managed 2.0 as Bean Type.
  2. The option Use Database is checked by default. Retain this setting.
  3. Enter com.sample.cmr.Customer as the name of the bean class. This fills the EJB Name as Customer, with JNDI Name as Customer_PiggyBankEJB_PiggyBank, automatically.
  4. Leave the option Expert Mode unchecked.
  5. Click Next.

Creating CMFs

  1. Select custres as the Resource Name.
  2. Select Table Filter as CUST* and click Get. This filters all tables starting with CUST and displays them in the Tables field.
  3. Select CUSTOMER table.
  4. Click Next.

Creating CMRs

The Container Managed Relationship panel is used to create relationships among beans. In this sample, Account and Customer bean are related as follows:

  1. Under Role 1, enter accountcmr as the CMR field name.
  2. Select 1->1 as the Relation. This means Customer has a unidirectional relationship with Account. The multiplicity of this relationship is one to one.
  3. Check option Cascade Delete. This automatically checks Role 2.
Note: Cascade Delete implies deleting Customer will delete Account too.
  1. Under Role 2, select Account as the EJB Name.
  2. Click Add to create the relationship between Account and Customer.

Add the JOIN condition

Now the relationship is added, add the Join condition to it as follows:

  1. Click on the icon listed under the field Join in the table to open the Join Condition panel.
  2. On the left, select CUSTOMER table and CUSTOMERID field.
  3. On the right, select ACCOUNT table and CUSTOMERID field.
  4. Click Add to add the Join condition to the relationship.
  5. Click OK to return to the Container Managed Relationship screen.
  6. Click Next.

Defining Create methods

  1. Move all rows from the Fields in Bean field using the right arrow.
  2. Click on plus to generate an ejbCreate method with CMF fields as the parameters.
  3. Click Next.

Finder Methods

No finder methods are required in Customer bean, click Finish.

Components generated by Bean Wizard

The bean wizard generates the code for Customer bean and stores it under the EJB module, PiggyBankEJB on the desk, PiggyBank.

Compiling the beans

When the beans and their interfaces are generated, they are compiled automatically. To force compile Account and Customer beans, right click on the EJB module PiggyBankEJB and select Clean Build.

Writing and using the JSP

JSP technology enables the generation of dynamic content for a web client. The sample JSP looks up Account and Customer beans, and displays customer details for a given account ID. JSP is created in Studio through standard templates.

    1. Right click on Web module on the desk and select New File.
  1. Select file type as JSP and click OK.

The JSP template panel requires the following information:

    1. Enter the filename as /index.jsp. The prefix `/' ensures the JSP is created under the context root of the application.
  1. PiggyBankWeb is selected by default as the Web module.
  2. Select LookupJsp as the template. This template includes a standard skeleton for looking up beans.
  3. Click OK.

Modifying the JSP code to look up beans

The JSP skeleton that is generated has standard place holders for business logic code. For the JSP to look up the bean and display details, the skeleton is modified as shown below:

Replace the line

<%@  
page import="javax.ejb.*,javax.naming.*,java.rmi.*,java.util.*" 
%> 

with

<%@  
page import="javax.ejb.*,javax.naming.*,java.rmi.*, java.util.*,com.sam-
ple.cmr.*" 
%> 

Enter the following line of code

<% out.println("CMR Sample Demonstration");  

before

InitialContext ic = null; 

Replace the section

// replace the DemoBean with the actual bean name 
// DemoHome demoHome = 
// (DemoHome)ic.lookup("java:comp/env/ejb/DemoBean"); 
// Demo demo = (Demo)demoHome.create(); 

with

CustomerLocalHome custlocalHome = (CustomerLocalHome)ic.lookup("java:comp/env/ejb/CustomerLocal");

String customerId = request.getParameter("customerid");

if(customerId!=null)

{

CustomerLocal custlocal = (CustomerLocal)custlocalHome.findByPrimaryKey(customerId);

AccountLocal acclocal = AccountLocal)custlocal.getAccountcmr();}

Add the following lines of code in the JSP after the above section. These lines are executed after the look up succeeds.

%>  
<h2><%  
out.println("CMR Sample Demonstration");  
if(!lookupFailed){  
%><table border=0 cellspacing=5>  
<tr><td><%out.println("Customer ID : " );%></td><td><% out.println(custlo-
cal.getCustomerid());%></td></tr>  
<tr><td><%out.println("Customer Name : "); %></td><td><% out.println(custlo-
cal.getName());%></td></tr>  
<tr><td><%out.println("Account No : " );%></td><td><% out.println(acclo-
cal.getAccountno());%></td></tr>  
<tr><td><%out.println("Account Balance : "); %></td><td><% out.println(acclo-
cal.getBalance());%></td></tr>  
</table>  
<%} 

Since the above lines should be included in the try block, ensure they are followed by the closing parenthesis of the try statement.

Replace the section

<FORM METHOD='POST'> 
<INPUT TYPE='SUBMIT' NAME='Submit' VALUE='Submit'> 
</FORM>  

with

<FORM METHOD="GET" > 
<h3>Enter Customer ID</h3> 
<INPUT TYPE="TEXT" NAME="customerid" SIZE="25"> 
<INPUT TYPE="SUBMIT" VALUE="SUBMIT"> 
<INPUT TYPE="HIDDEN" NAME="first" VALUE ="YES"> 
</FORM> 

Modify the user classpath

For the JSP file to look up the bean, the `/'classes directory should be added to the user classpath.

  1. Right click on PiggyBankWeb and select Properties.
  2. Select the Build tab.
  3. Click plus to start a file browser. Select directories along the following classpath: c:/pramati_studio/PiggyBank/classes/PiggyBankEJB
  4. Click OK to add the classpath to the user classpath field in Properties.
  5. Click OK.

Compile the JSP

Right click on the JSP listed in the desk and select Compile. Compiling checks the JSP for syntax errors.

Deploy the Application

To run the application, both EJB and Web modules have to be deployed.

Deploy EJB module

Right click on PiggyBankEJB on the desk and select Deploy. This directly deploys the EJB module on the server configured as the development server. By default, this is built-in Pramati Server.

Add EJB reference to the Web module

  1. Right click on PiggyBankWeb on the desk and select web.xml to start editing properties of the Web module.
  2. In the Web module properties tree, select References node.
  3. Under EJB References tab, add a reference to PiggyBankBean with ejb/CustomerLocal as the name.
  4. Select Entity as the bean type.
  5. Select the Local Reference tab.
  6. Select com.sample.CustomerLocalHome as the Local Home Class name. This automatically fills com.sample.CustomerLocal as the Local Class name with JNDI name as Customer_PiggyBankEJB_PiggyBank.
  7. Click Add to add the bean reference to the Web module.

Deploy Web module

Right click on PiggyBankWeb on the desk and select Deploy to start Deploy Tool. Before deploying the Web module:

  1. Click on the green arrow on the bottom right corner of Deploy Tool.
  2. In the EJB References panel, select the Link Name as Customer_PiggyBankEJB_PiggyBank. The module is now ready to be deployed.
  3. Select Archive > Deploy from the Deploy Tool menu to deploy the Web module.
  4. Click Exit.

Once both modules have been deployed, the CMP 2.0 application PiggyBank is ready to be accessed by the JSP client.

Running the JSP

To run the JSP, right click on it in the explore panel and select Run. This starts the browser that is configured for the IDE and runs the JSP in it.

The JSP can be run from a browser by using the following URL:

http://127.0.0.1:8181/PiggyBankWeb/index.jsp  
Note: In the URL, the Web module PiggyBankWeb is the context root of the application.
Accessing the application

The application prompts the user for the Customer ID. On entering the ID for example, 100, the application displays the related details from the Account and Customer tables in the database.


Pramati Technologies  © Copyright   TOCPREVNEXTINDEX