Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Writing Enterprise Applications with J2EE (Third lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)"— Presentation transcript:

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

2 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

3 Interacting Components In 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.

4 CalcHome The 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; }

5 Calc The calcBonus method is changed to take the social security number as a parameter. This way CalcBean 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.

6 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; }

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

8 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(); }...

9 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() { } }

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

11 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(" Bonus Calculation "); out.println(" Soc Sec retrieved: " + record.getSocSec() + " "); out.println(" Bonus Amount retrieved: " + record.getBonus() + " "); out.println(" "); } catch (javax.ejb.DuplicateKeyException e) {...}

12 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

13 Create a new Application Application display name: 2BeansApp Locate the directory for the.ear file File name: 2BeansApp.ear

14 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

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


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

Similar presentations


Ads by Google