Writing Enterprise Applications with J2EE (Third lesson)

15
Writing Enterprise Applications with J2EE (Third lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

description

Writing Enterprise Applications with J2EE (Third lesson). Alessio Bechini June 2002 (based on material by Monica Pawlan). The example in the previous lessons is modified, making the session bean look up and create the entity bean. - PowerPoint PPT Presentation

Transcript of Writing Enterprise Applications with J2EE (Third lesson)

Page 1: Writing Enterprise Applications with J2EE (Third lesson)

WritingEnterprise Applications

with J2EE(Third lesson)

Alessio Bechini

June 2002(based on material by Monica Pawlan)

Page 2: Writing Enterprise Applications with J2EE (Third lesson)

Cooperating Enterprise Beans

The example in the previous lessons is modified, making the session bean look up and create the entity bean.

Because the session and entity bean work together, they are bundled into one JAR file for eployment.

Required steps:• Change the Session Bean • Change the Servlet• Compile• Start the Platform and Tools• Assemble, Verify and Deploy• Run the J2EE Application

Page 3: Writing Enterprise Applications with J2EE (Third lesson)

Interacting ComponentsIn the example presented here, the entity bean is a client of the session bean. This means the entity bean gets its data from the session bean instead of from BonusServlet.

So, the calcBonus method

in the session bean

is modified to take

the social security number

as a parameter

and create the entity bean.

Page 4: Writing Enterprise Applications with J2EE (Third lesson)

CalcHomeThe CalcHome interface is unchanged.

It has the same create method that returns an instance of the remote interface.

package Beans;

import java.rmi.RemoteException;

import javax.ejb.CreateException;

import javax.ejb.EJBHome;

public interface CalcHome extends EJBHome {

Calc create() throws CreateException, RemoteException;

}

Page 5: Writing Enterprise Applications with J2EE (Third lesson)

CalcThe calcBonus method is changed to take the social security number as

a parameter. This wayCalcBean can pass the bonus and social security number to the entity bean.

A new getRecord method is added so CalcBean can find an entity bean by its primary key (the social security number).

Also, the calcBonus method signature throws DuplicateKeyException and CreateException, and BonusServlet can catch and handle either of them.

DuplicateKeyException descends from CreateException. If you design the calcBonus method to throw DuplicateKeyException, but catch CreateException, DuplicateKeyException is not thrown. The way around this is to have calcBonus throw both DuplicateKeyException and CreateException.

Page 6: Writing Enterprise Applications with J2EE (Third lesson)

Calc (Code)

package Beans;

import javax.ejb.EJBObject;import java.rmi.RemoteException;import javax.ejb.DuplicateKeyException;import javax.ejb.CreateException;

public interface Calc extends EJBObject { public Bonus calcBonus(int multiplier, double bonus, String socsec) throws RemoteException, DuplicateKeyException, CreateException; public Bonus getRecord(String socsec) throws RemoteException;}

Page 7: Writing Enterprise Applications with J2EE (Third lesson)

CalcBean

The code to create the entity bean is moved from BonusServlet to the calcBonus method so the bonus and social security number can be written to the entity bean after the bonus is calculated.

The homebonus variable is an instance variable: it can be used in the calcBonus method to look up the entity bean and in the getRecord method to locate the entity bean corresponding to the social security number.

Page 8: Writing Enterprise Applications with J2EE (Third lesson)

CalcBean (Code, I)package Beans;

import java.rmi.RemoteException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import javax.ejb.DuplicateKeyException;

import javax.ejb.CreateException;

public class CalcBean implements SessionBean {

BonusHome homebonus;//Throw DuplicateKeyException and CreateException

//so BonusServlet can catch and handle these exception conditions.

public Bonus calcBonus(int multiplier,double bonus, String socsec)

throws DuplicateKeyException, CreateException {

Bonus theBonus = null;

double calc = (multiplier*bonus);

try {

InitialContext ctx = new InitialContext();

Object objref = ctx.lookup("bonus");

homebonus = (BonusHome)

PortableRemoteObject.narrow(objref, BonusHome.class);

} catch (Exception NamingException) { NamingException.printStackTrace(); }

...

Page 9: Writing Enterprise Applications with J2EE (Third lesson)

CalcBean (Code, II)// store data in entity bean try { theBonus=homeBonus.create(calc, socsec); } catch (java.rmi.RemoteException e) { String message=e.getMessage(); e.printStackTrace(); } return theBonus;

public Bonus getRecord(String socsec) { Bonus record = null;// Use primary key to retrieve data from entity bean try { record=homeBonus.findByPrimaryKey(socsec); } catch (java.rmi.RemoteException e) { String message=e.getMessage(); } catch (javax.ejb.FinderException e) { e.printStackTrace(); } return record; } public void ejbCreate() { } public void setSessionContext(SessionContext context) { } public void ejbRemove() { } public void ejbActivate() { } public void ejbPassivate() { } public void ejbLoad() { } public void ejbStore() { }

}

Page 10: Writing Enterprise Applications with J2EE (Third lesson)

Servlet Code: init method

public class BonusServlet extends HttpServlet { CalcHome homecalc; BonusHome homebonus; Bonus theBonus, record;

public void init(ServletConfig config) throws ServletException{ try { InitialContext ctx = new InitialContext(); Object objref = ctx.lookup("calcs"); homecalc = (CalcHome)PortableRemoteObject.narrow(objref, CalcHome.class); } catch (Exception NamingException) { NamingException.printStackTrace(); } }...

The BonusServlet is very similar to the version of the previous lesson, with changes in the init and doGet methods.

The init method now looks up the CalcBean session bean only.

Page 11: Writing Enterprise Applications with J2EE (Third lesson)

Servlet Code: inside doGet (I)try {

Calc theCalculation;

//Retrieve Bonus and Social Security Information

String strMult = request.getParameter("MULTIPLIER");

//Calculate bonus

Integer integerMult = new Integer(strMult);

multiplier = integerMult.intValue();

socsec = request.getParameter("SOCSEC");

double bonus = 100.00;

theCalculation = homeCalc.create();

//Call session bean

theBonus = theCalculation.calcBonus(multiplier, bonus, socsec);

record = theCalculation.getRecord(socsec);

//Display data

out.println("<H1>Bonus Calculation</H1>");

out.println("<P>Soc Sec retrieved: " + record.getSocSec() + "<P>");

out.println("<P>Bonus Amount retrieved: " + record.getBonus() + "<P>");

out.println("</BODY></HTML>");

} catch (javax.ejb.DuplicateKeyException e) {...}

Page 12: Writing Enterprise Applications with J2EE (Third lesson)

Assemble the J2EE Application

The steps for assembling the new application, making use of the deploy tool, include the following:

• Create a new J2EE Application• Create a new Web Component,

with the new version of the servlet• Bundle Session and Entity Beans

in one JAR file

Page 13: Writing Enterprise Applications with J2EE (Third lesson)

Create a new Application

Application display name:

2BeansApp

Locate the directoryfor the .ear file

File name:

2BeansApp.ear

Page 14: Writing Enterprise Applications with J2EE (Third lesson)

Create a new Web Component

Using the proper wizard, a web component (bundled into a WAR file) containing bonus.html and BonusServlet.class is created, according to the following data:

WAR display name:

BonusWar

Servlet class:

BonusServlet.class

Component Aliases:

BonusAlias

Page 15: Writing Enterprise Applications with J2EE (Third lesson)

The JAR File for the EJBs

To put both the session and entity beans in the same JAR file,we can first create a jar for the session bean, and then add the entity bean to it.

Note that transactions are required to be associated with methods

calcBonus and getRecord of the session bean CalcBean.