Chapter 9: JSP and JavaBeans - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Chapter 9: JSP and JavaBeans

Description:

... useBean id='newsfeed' class='com.wrox.begjsp.ch2.NewsFeed' scope='request' ... jsp:useBean id='biderror' class='com.wrox.begjsp.ch09.BidError' scope='request' ... – PowerPoint PPT presentation

Number of Views:144
Avg rating:3.0/5.0
Slides: 14
Provided by: conest
Category:
Tags: jsp | javabeans | chapter | wrox

less

Transcript and Presenter's Notes

Title: Chapter 9: JSP and JavaBeans


1
Chapter 9 JSP and JavaBeans
Reference Beginning JSP
2
Objectives
  • What are JavaBeans?
  • JavaBean Usage within JSPs
  • Access JavaBeans with Standard Actions
  • Access JavaBeans with EL
  • Access JavaBeans from scriptlets
  • Front Controller JSPs
  • JSP Input Validation
  • Error Message JavaBean

3
Review JSP Action and JavaBeans
  • Reference Pages 55 - 58
  • JavaBeans (more details in Chapter 9)
  • Reusable software components that has properties,
    methods and events
  • Can be used to generate dynamic content for the
    portal pages
  • To create a JavaBean attached to the current
    request
  • ltjspuseBean idnewsfeed" class"com.wrox.begjsp.
    ch2.NewsFeed
  • scope"request"gtlt/jspuseBeangt
  • To set the topic property to news
  • ltjspsetProperty name"newsfeed"
    property"topic" value"news"/gt
  • To get the value property
  • ltjspgetProperty name"newsfeed"
    property"value"/gt

4
What Are JavaBeans?
  • Javas software component model specification
  • Written in Java platform neutral
  • JavaBean Characteristics
  • Must have a default (zero-argument) constructor
  • Used when restoring JavaBean from a file
  • Must be serializable
  • Implements java.io.Serializable
  • Can save JavaBean to a file
  • Note Many non-visual JavaBeans do not conform to
    this requirement.
  • Private instance variables
  • Public features (methods, properties, events)

5
JavaBean Property Types
  • Simple
  • Use standard getter/setter signatures
  • E.g., public String getName()
  • Boolean
  • Use isltPropertyNamegt for getter method
  • E.g., public boolean isValid()
  • Better use getValid() to allow access using EL
  • Indexed
  • Represents an array of values
  • Use java.util.ArrayList for dynamic arrays
  • E.g., public ArrayList getValues()

6
Coding a JavaBean
  • Reference Page 276-284
  • NetBeans 4.1
  • Use wizard to create getter/setter methods
  • E.g., Bid.java
  • Add instance variable and read/write access
  • private String item
  • public String getItem() return this.item
  • public void setItem(String item) this.item
    item
  • JavaBean method
  • Can be wrapped as an EL function
  • public double calculateTotal ()
  • return price quantity

7
JavaBean as Data Transfer Objects
  • Reference Chapter 9, Example 1
  • JavaBeans carry data between pages
  • Between business logic and presentation layer.
  • index.jsp (controller JSP)
  • Creates Bid JavaBean instance named bidinfo
  • ltjspuseBean id"bidinfo" class"com.wrox.begjsp.c
    h09.Bid" scope"request"gt
  • Sets property values
  • ltjspsetProperty name"bidinfo" property""/gt
  • showbid.jsp (presentation JSP)
  • Access bidinfo property values
  • Using EL bidinfo.item
  • Using Scriptlet lt bidinfo.getPrice() gt

8
Front Controller JSPs
  • Reference Page 284-288, Example 1
  • Front controller (e.g., index.jsp) switches
    incoming request to different presentation JSPs
  • Incoming request from enterbid.jsp
  • ltinput type"hidden" name"action" value"bid"/gt
  • redirects request to appropriate JSP page
  • ltcwhen test"empty param.action"gt
  • ltjspforward page"enterbid.jsp"/gt
  • lt/cwhengt
  • Perform common tasks such as input validation
  • ltcif test"(param.price lt 0) (param.price
    gt 999)"gt

9
JSP Input Validation
  • Reference Page 288-291, Example 1
  • Create BidError JavaBean with msg property
  • index.jsp creates BidError instance
  • ltcif test"(param.price lt 0) (param.price
    gt 999)"gt
  • ltjspuseBean id"biderror" class"com.wrox.begjs
    p.ch09.BidError" scope"request"gt
  • ltjspsetProperty name"biderror"
    property"msg" value"Sorry, your bid is not in
    range. Please enter again."/gt
  • enterbid.jsp displays input error
  • ltcif test"!(empty biderror)"gt
  • lttrgtlttd class"errorText" colspan"2"gt
  • biderror.msg lt/tdgt
  • lt/trgt
  • lt/cifgt

10
Input Validation Error Example
  • Reference Chapter 9, Example 1
  • Enter a bid higher than 999 (e.g, 2000)

11
How to Remember Bid Price Entered
  • index.jsp set bidinfo properties before forward
    enterbid.jsp
  • enterbid.jsp add value attribute to ltinputgt tag
  • ltinput name"price" type"text"
    value"bidinfo.price" /gt

12
Testing ltjspuseBeangt Behavior
  • In index.jsp, change scope of bidinfo from
    request to session
  • ltjspuseBean id"bidinfo" class"com.wrox.begjsp.c
    h09.Bid" scope"session"gt
  • Findings
  • Only the first bid price is remembered in bidinfo
  • ltjspuseBeangts body is executed only if a new
    bean instance is created.
  • Result ltjspsetPropertygt is executed the first
    time bidinfo is created.
  • Solution
  • Add ltcremove var "bidinfo"/gt before
    ltjspuseBeangt

13
Next Steps
  • Try It Out sections of Textbook (Chapter 9)
  • Example 1
  • Exercise 1
  • You can skip Example 2 and Exercise 2
  • Do Assignment 3
Write a Comment
User Comments (0)
About PowerShow.com