1 / 15

Writing Enterprise Applications with J2EE

Learn how to modify session and entity beans, bundle them into a JAR file, and deploy a J2EE application. Includes code examples.

vanida
Download Presentation

Writing Enterprise Applications with J2EE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 CooperatingEnterprise Beans

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

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

  5. The 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. Calc

  6. 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; } Calc (Code)

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

  8. 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(); } ... CalcBean (Code, I)

  9. // 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() { } } CalcBean (Code, II)

  10. 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(); } } ... Servlet Code: init method 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. 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) {...} Servlet Code: inside doGet (I)

  12. Assemble the J2EEApplication 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 directoryfor 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.

More Related