JSP Application Design - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

JSP Application Design

Description:

Has the advantage of auto-capturing form data. Also: ... And then those values used in other pages. Convenient for page inclusion and forwarding ... – PowerPoint PPT presentation

Number of Views:453
Avg rating:3.0/5.0
Slides: 18
Provided by: simon175
Category:

less

Transcript and Presenter's Notes

Title: JSP Application Design


1
JSP Application Design
  • CC292
  • Simon M. Lucas

2
Main Features of JSP
  • Get / Set properties
  • Via getX setX naming patterns
  • Has the advantage of auto-capturing form data
  • Also
  • Other data fields on a Java Object can be set
  • And then those values used in other pages
  • Convenient for page inclusion and forwarding

3
Model View Controller (MVC)
  • The MVC architecture is one way to approach web
    application design
  • This uses a controller page to handle all
    requests
  • The controller reads any query or form parameters
  • Decides on which action to take
  • Then dispatches control to another page to take
    the action

4
MVC Architecture
Request Proc.jsp?pageAccountidsml
Proc.jsp
Action1.jsp
Account.jsp
Checkout.jsp
5
MVC Continued
  • Often implemented with Java Servlets
  • But can be done just as easily with JSP
  • And Java support classes where needed
  • In previous example
  • Proc.jsp would extract the Account info for the
    given id
  • Then pass control to the Account.jsp page
  • Having set up the appropriate values (perhaps as
    Beans)

6
Forward and Include
  • Two ways to pass control to another JSP page
  • ltjspforward pageAccount.jspgt
  • Terminates execution of current page
  • Transfers control to the forwardee
  • ltjspinclude pageAccount.jspgt
  • Executes the specified page
  • Then returns control to the current page

7
JSP Include Example
  • JSP include very widely used
  • Include dynamic data components in a calling page
    see below

Banner
Index
Basket
Products
8
Taking Care with Beans
  • To be passed from caller to callee, use the
    appropriate scope
  • Usually, request
  • Page would be restricted to the calling page and
    would not work.
  • Scope must be the same for caller and callee!
  • Be careful about using Application or Session
    scope why?

9
Explanation
  • idtable this sets up a variable name that
    can be used later on the same page, and
    referenced with ltuseBeangt on other pages
  • classtable.Table the bean will be
    instantiated to be an object of this Class
  • scoperequest want it to be passed from
    caller to callee

10
Table Example Caller
  • ltjspuseBean id"table" class"table.Table"
    scope"request"/gt
  • lthtmlgt
  • ltheadgtlttitlegtSimple JSP page to test a Generic
    Tablelt/titlegtlt/headgt
  • ltbodygt
  • lth2gtIncluded Table Testlt/h2gt
  • lt // note cannot use table new ... here -
  • // would not get passed to the included
    page
  • // but table.table new ... is fine
  • table.table new
  • StudentTable(ReadStudents.readStudents(
  • "c/tomcat5/webapps/test/data/students.t
    xt")) gt
  • ltjspinclude page"Table.jsp" /gt

11
Table Example Callee (Table.jsp)
  • ltjspuseBean id"table" class"table.Table"
    scope"request"/gt
  • lttablegt
  • lt--
  • First do the table head, then all the rows
  • --gt
  • lttrgt
  • lt
  • for (Object data table.table.getHead
    er().getData())
  • gt
  • ltthgtlt data gtlt/thgt
  • lt
  • gt
  • lt/trgt
  • ... // continued for rows

12
Table.jsp (continued now the rows)
  • lt
  • for (Row row table.table.getRows()) gt
    lttrgtlt
  • for (Object data row.getData())
  • gt lttdgtlt data gtlt/tdgt lt
  • gtlt/trgt lt
  • gt
  • lt/tablegt

13
Sample Output(CSS used but not in these slides)
14
But Required Some Java behind the Scenes
  • Table.java
  • package table
  • public class Table
  • // use a concrete class to pass the data
    interface
  • public Tabular table

15
Tabular Interface
  • Can be implemented by various concrete classes to
    deliver data from data sources
  • package table
  • import java.util.ArrayList
  • public interface Tabular
  • public Row getHeader()
  • public ArrayListltRowgt getRows()

16
Row Interface
  • Study this code this is the end of the object
    model data cells simply returned as Strings
  • package table
  • import java.util.ArrayList
  • public interface Row
  • ArrayListltStringgt getData()

17
Summary
  • Key JSP concepts
  • Forward and Include
  • Beans and Bean Scope
  • Study Table example
  • Worked with request scope
  • Question What are the advantages and
    disadvantages of generic tables versus writing a
    specific JSP page for each table?
Write a Comment
User Comments (0)
About PowerShow.com