Writing Enterprise Applications with J2EE Fifth lesson - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Writing Enterprise Applications with J2EE Fifth lesson

Description:

We can put a JavaBean between the JSP page. and CalcBean session bean ... BODY BGCOLOR = 'WHITE' HEAD TITLE Bonus Calculation /TITLE /HEAD BLOCKQUOTE ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 18
Provided by: www2In
Category:

less

Transcript and Presenter's Notes

Title: Writing Enterprise Applications with J2EE Fifth lesson


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

2
Using JavaBeans with EJBs
  • Rationale for using JavaBeans
  • We can put a JavaBean between the JSP page and
    CalcBean session bean to obtain the Model, View,
    Controller (MVC) separation.
  • MVC is a design pattern the Model provides the
    application business logic, the View is its
    screen presentation, and the Controller is an
    object that manages user interactions with the
    View.
  • In the previous example, the HTML and JSP pages
    both provide the View and manage user
    interactions with the data (Controller). The
    entity and session bean (BonusBean and CalcBean)
    are the application objects (or Model).

3
Separation of View and Controller
  • In the current example, we are going to use a
    JSP page for the screen presentation (View), a
    JavaBean to manage what happens when the user
    interacts with the View (Controller), and the
    entity and session beans for the application
    objects (Model).
  • Separating the Controller from the View lets the
    JavaBean serve as a wrapper for the session bean
    and gives the example a much cleaner MVC
    separation.
  • An application that uses clear design patterns
    is easier to update, maintain, and manage.

4
Steps towards MVC Separation
  • Create bonus.jsp
  • Create the JavaBeans Class
  • Bean Properties
  • Remove the WAR File
  • Create New WAR FIle
  • Verify and Deploy the J2EE Application
  • Run the J2EE Application

5
Merging bonus.html and Bonus.jsp
  • In the previous example, the HTML form calls the
    JSP page when the user clicks the Submit button
    on the HTML page.
  • Another way to create the user interface is with
    one single JSP page that includes the HTML form,
    JSP scriptlets, and JSP-specific tags for
    interacting with the JavaBean. When the JSP page
    loads, the HTML form is displayed and the
    scriptlet and JSP-specific tags are executed.
  • After the user enters some data and clicks the
    Submit button, the HTML form is redisplayed and
    the scriptlet and JSP-specific tags execute again
    with the supplied data. This is because the
    ACTION parameter for the HTML form cursively
    calls the JSP page itself
  • ...
  • lt!--ACTION parameter calls this page--gt
  • ltFORM METHOD"GET" ACTION"bonus.jsp"gt
  • ...

6
Create Bonus.jsp
  • The code for bonus.jsp is now simplified,
    because the code to look up the session bean
    and calculate the bonus is now in the JavaBean.
  • The first part of the file contains the HTML code
    to create the form. The code to pass the HTML
    form data to the JavaBean is in the second part
    of the file.
  • ltHTMLgt
  • ltBODY BGCOLOR "WHITE"gt
  • ltHEADgt ltTITLEgtBonus Calculationlt/TITLEgt lt/HEADgt
  • ltBLOCKQUOTEgt
  • ltH3gtBonus Calculationlt/H3gt
  • lt!--ACTION parameter calls this page--gt
  • ltFORM METHOD"GET" ACTION"bonus.jsp"gt
  • ltPgt Enter social security Number
  • ltPgt ltINPUT TYPE"TEXT" NAME"SOCSEC"gtlt/INPUTgt
  • ltPgt Enter Multiplier
  • ltPgt ltINPUT TYPE"TEXT" NAME"MULTIPLIER"gtlt/INPUTgt
  • ltPgt ltINPUT TYPE"SUBMIT" VALUE"Submit"gt ltINPUT
    TYPE"RESET"gt
  • lt/FORMgt
  • ...

7
Bonus.jsp (cont.)
  • ...
  • lt!--Scriptlet and JavaBeans Tags start here --gt
  • ltjspuseBean id "jbonus" class "JBonusBean"/gt
  • lt! String sMult, ssec gt
  • lt
  • sMult request.getParameter("MULTIPLIER")
  • ssec request.getParameter("SOCSEC")
  • gt
  • ltjspsetProperty name "jbonus"
    property"strMult" value"ltsMultgt"/gt
  • ltjspsetProperty name "jbonus"
    property"socsec" value"ltssecgt"/gt
  • Social security number retrieved
  • ltjspgetProperty name"jbonus" property"socsec"/gt
  • ltPgt
  • Bonus Amount retrieved
  • ltjspgetProperty name"jbonus" property"bonusAmt"
    /gt
  • ltPgt

8
Specify the JavaBean
  • The following HTML tag specifies the JavaBean
    being used in this example.
  • The id parameter defines an alias to use to
    reference the JavaBean, and the class parameter
    specifies the JavaBeans class.
  • In this example the id is jbonus and the class
    is JBonusBean.
  • ltjspuseBean id "jbonus"
  • class "JBonusBean"/gt

9
Get the Data
  • The following JSP scriptlets retrieve the
    user-supplied data from the HTML form input
    fields.
  • The multiplier is stored in the sMult String
    variable, and the social security number is
    stored in the ssec String variable.
  • lt! String sMult, ssec gt
  • lt
  • sMult request.getParameter("MULTIPLIER")
  • ssec request.getParameter("SOCSEC")
  • gt

10
Pass Data to the JavaBean
  • The following tags set two properties in the
    JavaBean. A property is a private field in the
    JavaBean class.
  • The first line uses the jspsetProperty name tag
    to set the strMult field in the JBonusBean class
    (aliased by the jbonus id) to the value stored
    in the sMult variable.
  • The second line performs a similar operation for
    the socsec field in the JBonusBean class.
  • The value"ltssecgt" expression sends the data
    contained in the ssec variable to the socsec
    field in the JavaBean.
  • ltjspsetProperty name "jbonus"
    property"strMult" value"ltsMultgt"/gt
  • ltjspsetProperty name "jbonus"
    property"socsec" value"ltssecgt"/gt

11
Retrieve Data from the JavaBean
  • Retrieving data from a JavaBean is similar to
    sending data to it. You use the jspgetProperty
    name tag and indicate the property (private
    field) whose data you want to get.
  • The following getProperty name tag retrieves the
    data stored in the socsec private field of the
    JBonusBean class (aliased by the jbonus id).
  • Social security number retrieved
  • ltjspgetProperty name"jbonus" property"socsec"/gt

12
Create the JavaBeans Class
  • A Java bean looks just like any ordinary
    Javaclass. But to be a bean, a Java class must
    follow a set of simple naming and design
    conventions. Because beans follow the JavaBeans
    specification, they can be accessed and managed
    by other programs and tools that follow the same
    conventions.
  • In the previous example with bonus.jsp, HTML tags
    and JSP scriptlets are used to get and set data
    in the private fields of the JBonusBean class.
    This is possible because JBonusBean follows
    the JavaBeans naming and design conventions.

13
The JBonusBean class (I)
  • import javax.naming.
  • import javax.rmi.PortableRemoteObject
  • import Beans.
  • public class JBonusBean
  • private String strMult, socsec, message
  • private double bonusAmt
  • CalcHome homecalc
  • public JBonusBean()
  • try
  • InitialContext ctx new InitialContext()
  • Object objref ctx.lookup("calcs")
  • homecalc (CalcHome)PortableRemoteObject.na
    rrow(objref,

  • CalcHome.class)
  • catch (javax.naming.NamingException e)
    e.printStackTrace()

14
The JBonusBean class (II)
  • public double getBonusAmt()
  • if(strMult ! null)
  • Integer integerMult new Integer(strMult)
  • int multiplier integerMult.intValue()
  • try
  • double bonus 100.00
  • Calc theCalculation homecalc.create()
  • Bonus theBonus theCalculation.calcBonus(
    multiplier, bonus, socsec)
  • Bonus record theCalculation.getRecord(so
    csec)
  • bonusAmt record.getBonus()
  • socsec record.getSocSec()
  • catch (javax.ejb.DuplicateKeyException e)
  • message e.getMessage()
  • catch (javax.ejb.CreateException e)
    e.printStackTrace()
  • catch (java.rmi.RemoteException e)
    e.printStackTrace()
  • return this.bonusAmt
  • else this.bonusAmt 0 this.message
    "None." return this.bonusAmt
  • public String getMessage() return
    this.message

15
Bean Properties
  • Bean properties define the data accessible to
    other programs and tools through get and set
    methods..
  • Properties are actually private class fields that
    should always be private and only accessible
    through get and set methods.
  • The following code segment shows the private
    properties for the JBonusBean class. This class
    has a corresponding getltpropertygt method for
    each field and corresponding setltpropertygt
    methods for the strMult and socsec fields.
  • public class JBonusBean
  • private String strMult, socsec, message
  • private double bonusAmt
  • ...

16
JBonusBean constructor
  • The JBonusBean constructor looks up the session
    Bean.
  • public JBonusBean()
  • try
  • InitialContext ctx new InitialContext()
  • Object objref ctx.lookup("calcs")
  • homecalc (CalcHome)PortableRemoteObject.narro
    w(
  • objref, CalcHome.class)
  • catch (javax.naming.NamingException e)
  • e.printStackTrace()

17
Set Methods
  • Setter methods set properties (private fields) to
    specified values. setSocsec and setStrMult are
    in charge ofr setting the socsec and strMult
    private fields (JavaBean properties).
  • The values used to set the socsec and strMult
    properties come from the setProperty name tags
    in the JSP page. The J2EE server uses the
    information in the following setProperty name
    tags to locate the corresponding set methods in
    the JBonusBean (aliased by the jbonus id)
  • Similar observations apply to get methods.
  • ltjspsetProperty name "jbonus"
    property"strMult

  • value"ltsMultgt"/gt
  • ltjspsetProperty name "jbonus"
    property"socsec"

  • value"ltssecgt" /gt
Write a Comment
User Comments (0)
About PowerShow.com