Writing Enterprise Applications with J2EE Fourth lesson - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Writing Enterprise Applications with J2EE Fourth lesson

Description:

... with servlet code segments embedded between various forms of ... FORM /BLOCKQUOTE ... bonus.html is to have the ACTION parameter in the HTML form invoke ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 16
Provided by: www2In
Category:

less

Transcript and Presenter's Notes

Title: Writing Enterprise Applications with J2EE Fourth lesson


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

2
Using JSPs instead of Servlets
  • JSPs let you put segments of servlet code
    directly into a static HTML page.
  • When the JSP Page is loaded by a browser, the
    servlet code executes and the application server
    creates, compiles, loads, and runs a background
    servlet to execute the servlet code segments and
    return an HTML page or print an XML report.
  • This lesson changes the WAR file to use a JSP
    Page instead of BonusServlet.
  • Required steps
  • Create the JSP Page
  • Change bonus.html
  • Remove the WAR File and create a new one
  • Verify, deploy and run the J2EE Application

3
Create the JSP Page
  • A JSP Page looks like an HTML page with servlet
    code segments embedded between various forms of
    leading (lt) and closing (gt) JSP tags.
  • There are no HttpServlet methods such as init,
    doGet, or doPost. Instead, the code that would
    normally be in these methods is embedded directly
    in the JSP Page using JSP scriptlet tags.
  • The following Bonus.jsp is equivalent to
    BonusServlet.
  • Note that JSP tags cannot be nested. For example,
    you cannot nest a JSP comment tag within a JSP
    scriptlet tag.

4
Bonus.jsp (I)
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtBonus Calculationlt/TITLEgt
  • lt/HEADgt
  • lt-- Comment Scriptlet for import statements lt_at_
    indicates a jsp directive --gt
  • lt_at_ page import"javax.naming." gt
  • lt_at_ page import"javax.rmi.PortableRemoteObject"
    gt
  • lt_at_ page import"Beans." gt
  • lt-- Comment Scriptlet to get the
    parameters,convert string to Integer to int for
    bonus calculation, and declare/initialize bonus
    variable. lt indicates a jsp scriptlet --gt
  • lt! String strMult, socsec gt
  • lt! Integer integerMult gt
  • lt! int multiplier gt
  • lt! double bonus gt
  • lt strMult request.getParameter("MULTIPLIER")
  • socsec request.getParameter("SOCSEC")
  • integerMult new Integer(strMult)
    multiplier integerMult.intValue()
  • bonus 100.00
  • gt
  • lt-- Comment Scriptlet to look up session Bean
    --gt

5
Bonus.jsp (II)
  • lt-- Comment Scriptlet to create session Bean,
    call calcBonus method,
  • and retrieve a database record by the social
    security number (primary key) --gt
  • lt try
  • Calc theCalculation homecalc.create()
  • Bonus theBonus theCalculation.calcBonus(mu
    ltiplier,bonus,socsec)
  • Bonus record theCalculation.getRecord(socs
    ec)
  • gt
  • lt-- Comment HTML code to display retrieved data
    on returned HTML page. --gt
  • ltH1gtBonus Calculationlt/H1gt
  • Social security number retrieved lt
    record.getSocSec() gt ltPgt
  • Bonus Amount retrieved lt record.getBonus() gt
    ltPgt
  • lt-- Comment Scriptlet to catch
    DuplicateKeyException --gt
  • lt catch (javax.ejb.DuplicateKeyException e)
  • String message e.getMessage()
  • gt
  • lt-- Comment HTML code to display original data
    passed to JSP on returned HTML page --gt
  • Social security number passed in lt socsec gt
    ltPgt
  • Multiplier passed in lt strMult gt ltPgt
  • Error lt message gt

6
Comments
  • The first seven lines of Bonus.jsp show straight
    HTML followed by a JSP comment.
  • JSP comments are similar to HTML comments
    except they start with lt-- instead of lt!--,
    which is how they look in HTML.
  • You can use either JSP or HTML comments in a JSP
    file.
  • HTML comments are sent to the clients web
    browser where they appear as part of the HTML
    page, and JSP comments are stripped out and do
    not appear in the generated HTML.

7
Directives
  • JSP directives are instructions processed by the
    JSP engine when the JSP Page is translated to a
    servlet.
  • The directives used in this example tell the JSP
    engine to include certain packages and classes.
  • Directives are enclosed by the lt_at_ and gt
    directive tags.
  • Example of directives in Bonus.jsp
  • lt_at_ page import"javax.naming." gt
  • lt_at_ page import"javax.rmi.PortableRemoteObject"
    gt
  • lt_at_ page import"Beans." gt

8
Declarations
  • JSP declarations let you set up variables for
    later use in expressions or scriptlets.
  • You can also declare variables within expressions
    or scriptlets at the time you use them.
  • The scope is the entire JSP Page, so there is no
    concept of instance variables. That is, you do
    not have to declare instance variables to be used
    in more than one expression or scriptlet.
  • Declarations are enclosed by lt! and gt.
  • You can have multiple declarations.
  • For example, lt! double bonus String text gt.

9
Scriptlets
  • JSP scriptlets let you embed java code segments
    into the JSP page.
  • The embedded code is inserted directly into the
    generated servlet that executes when the page is
    requested.
  • This scriptlet uses the variables declared in the
    directives described above.
  • Scriptlets are enclosed by the lt and gt
    scriptlet tags.
  • Example of a scriptlet in Bonus.jsp
  • lt
  • strMult request.getParameter("MULTIPLIER")
  • socsec request.getParameter("SOCSEC")
  • integerMult new Integer(strMult)
  • multiplier integerMult.intValue()
  • bonus 100.00
  • gt

10
Predefined Variables
  • A scriptlet can use the following predefined
    variables session, request, response, out, and
    in.
  • This example uses the request predefined
    variable, which is an HttpServletRequest object.
    Likewise, response is an HttpServletResponse
    object, out is a PrintWriter object, and in is a
    BufferedReader object.
  • Predefined variables are used in scriptlets in
    the same way they are used in servlets, except
    you do not declare them.
  • Example of use of predefined variables in
    Bonus.jsp
  • lt
  • strMult request.getParameter("MULTIPLIER")
  • socsec request.getParameter("SOCSEC")
  • ...

11
Expressions
  • JSP expressions let you dynamically retrieve or
    calculate values to be inserted directly into the
    JSP Page.
  • In this example, an expression retrieves the
    social security number from the Bonus entity bean
    and puts it on the JSP page.
  • Example of use of expressions in Bonus.jsp
  • ltH1gtBonus Calculationlt/H1gt
  • Social security number retrieved lt
    record.getSocSec() gt
  • ltPgt
  • Bonus Amount retrieved lt record.getBonus() gt
  • ltPgt

12
JSP-Specific Tags
  • The JavaServer Pages 1.1 specification defines
    JSP-specific tags that let you extend the JSP
    implementation with new features and hide
    complexity from visual designers who need to
    look at the JSP page and modify it.
  • The JSP example in this lesson does not use any
    JSP-specific tags.
  • The JSP-specific tags defined in the 1.1
    specification are the following
  • jspforward and jspinclude to instruct the JSP
    engine to switch from the current page to another
    JSP page.
  • jspuseBean, jspsetProperty, and jspgetProperty
    let you embed and use JavaBeans technology inside
    a JSP Page.
  • jspplugin automatically downloads the
    appropriate Java Plug-In to the client to execute
    an applet with the correct Java platform.

13
Change bonus.html
The only change you need to make to bonus.html is
to have the ACTION parameter in the HTML form
invoke Bonus.jsp instead of BonusServlet.
  • ltHTMLgt
  • ltBODY BGCOLOR "WHITE"gt
  • ltBLOCKQUOTEgt
  • ltH3gtBonus Calculationlt/H3gt
  • 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
  • lt/BLOCKQUOTEgt
  • lt/BODYgt
  • lt/HTMLgt

14
Create a new Web Component
  • Upon the removal of the old WAR file, we have to
    make a new one containing both Bonus.jsp and
    bonus.html.
  • WAR display name
  • BonusWar
  • Component file
  • Bonus.jsp

15
Run the J2EE Appl.
  • The web server runs on port 8000 by default. To
    open the bonus.html page point your browser to
  • http//localhost8000/JSPRoot/bonus.htm
    l
  • which is where the Deploy tool put the HTML file.
Write a Comment
User Comments (0)
About PowerShow.com