MIT AITI 2004 JSP Lecture 3 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

MIT AITI 2004 JSP Lecture 3

Description:

Usually every page of a site requires a common header, footer, and structure. ... BODY BGCOLOR='blue' Today's date: %=new Date()% /BODY /HTML ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 19
Provided by: GDe86
Category:
Tags: aiti | jsp | mit | bgcolor | lecture

less

Transcript and Presenter's Notes

Title: MIT AITI 2004 JSP Lecture 3


1
MIT AITI 2004JSP Lecture 3
  • Including and Forwarding

2
Shared Code
  • Usually every page of a site requires a common
    header, footer, and structure.
  • Suppose each page of a site is written as
  • lt_at_page import"java.util.Date"gt
  • ltHTMLgt
  • ltHEADgtltTITLEgtMy Pagelt/TITLEgtlt/HEADgt
  • ltBODY BGCOLOR"blue"gt
  • Todays date ltnew Date()gt
  • . . .
  • lt/BODYgt
  • lt/HTMLgt
  • Lots of duplicate HTML and JSP!

3
Static Include
  • Put common code into separate files
  • lt_at_page import"java.util.Date"gt
  • ltHTMLgt
  • ltHEADgtltTITLEgtMy Pagelt/TITLEgtlt/HEADgt
  • ltBODY BGCOLOR"blue"gt
  • Todays date ltnew Date()gt
  • . . .
  • lt/BODYgt
  • lt/HTMLgt

header.jsp
footer.html
  • becomes . . .
  • lt_at_include file"header.jsp" gt
  • . . .
  • lt_at_include file"footer.html" gt

4
Include Directive
  • lt_at_include file"filename"gt
  • Pastes text of file directly into the page before
    the page is compiled.
  • Beware of variable name clashes
  • Dont use in confusing ways
  • (for instance, included file ending an if
    statement)
  • We now know two directives
  • Include directive lt_at_include file"filename"gt
  • Page directive lt_at_page import"javaclass"gt

5
Dynamic Include
  • Another way to include
  • ltjspinclude page"header.jsp" /gt
  • . . .
  • ltjspinclude page"footer.html" /gt
  • Different from include directive!
  • Text of file NOT pasted in before compiling
  • Included page is called during execution

6
JSP Include Tag
  • How it looks
  • ltjspinclude page"filename" /gt
  • or
  • ltjspinclude page"filename"gt
  • lt/jspincludegt
  • How it works
  • Passes the request on to the included page
  • When done, returns to previous page

7
JSP Include Tag 2
  • Also send additional parameters to included page
  • ltjspinclude page"printname.jsp"gt
  • ltjspparam name"firstname" value"Greg" /gt
  • lt/jspincludegt
  • Can put JSP expressions into JSP tags
  • ltjspinclude page"ltpageVargt"gt
  • ltjspparam name"firstname"
    value"ltnameVargt" /gt
  • lt/jspincludegt

8
Static or Dynamic Include?
  • Try to use static include (include directive) if
    possible, because it is faster
  • Use dynamic inclusion (JSP include tag) if
  • Including large file with a lot of variables
  • (avoid potential variable name clashes)
  • Passing parameters to included page
  • The page you want to include is a variable
  • (include the result of a JSP expression)

9
Forwarding
  • JSP Forward Tag
  • ltjspforward page"filename" /gt
  • How it works
  • Passes the request on to the page forwarded to
  • When done, does not return to previous page, just
    stops processing the request.

10
JSP Forward Tag
  • Like jspinclude, you can . . .
  • Send additional parameters to forwarded page
  • ltjspforward page"printname.jsp"gt
  • ltjspparam name"firstname" value"Greg" /gt
  • lt/jspforwardgt
  • Put JSP expressions into JSP tags
  • ltjspforward page"ltpageVargt"gt
  • ltjspparam name"firstname"
    value"ltnameVargt" /gt
  • lt/jspforwardgt

11
Dynamic Include Example
  • print-two-names.jsp
  • lthtmlgt
  • ltbodygt
  • ltjspinclude page"printname.jsp"gt
  • ltjspparam name"firstname" value"Greg" /gt
  • lt/jspincludegtltpgt
  • ltjspinclude page"printname.jsp"gt
  • ltjspparam name"firstname" value"Eric" /gt
  • lt/jspincludegt
  • lt/bodygt
  • lt/htmlgt

12
Print Two Names Snapshot
13
Dynamic Forward Example
  • print-one-name.jsp
  • lthtmlgt
  • ltbodygt
  • ltjspforward page"printname.jsp"gt
  • ltjspparam name"firstname" value"Greg" /gt
  • lt/jspforwardgt
  • ltjspforward page"printname.jsp"gt
  • ltjspparam name"firstname" valueEric" /gt
  • lt/jspforwardgt
  • lt/bodygt
  • lt/htmlgt

14
Print One Name Snapshot
15
Request Parameters Limiting
  • Request parameters are limiting because they must
    be Strings
  • Must convert back and forth from Objects to
    String representation of Objects
  • String reps must be unique
  • Good news!
  • We can put Objects in the request too

16
Request Attributes
  • Objects stored in the request object
  • Add attribute with request.setAttribute
  • lt
  • request.setAttribute("dataString",
    dataObject)
  • gt
  • Get attribute with request.getAttribute
  • lt
  • DataType dataObject
  • (DataType)request.getAttribute("dataString")
  • gt
  • getAttribute returns Object, so must cast

17
Request Attribute Example
  • prepare-date.jsp
  • lt
  • int month Integer.parseInt(request.getParameter
    ("m"))
  • int day Integer.parseInt(request.getParameter("
    d"))
  • int year Integer.parseInt(request.getParameter(
    "y"))
  • Calendar cal Calendar.getInstance()
  • cal.clear()
  • cal.set(year, month, day)
  • request.setAttribute("date", cal.getDate())
  • gt
  • ltjspforward page"use-date.jsp" /gt
  • use-date.jsp
  • lt
  • Date date (Date)request.getAttribute("date")
  • . . .
  • gt

18
Include Forward Review
  • Static include
  • Include directive lt_at_include file"filename"gt
  • Pastes text into original file
  • Dynamic include
  • JSP include tag ltjspinclude page"file.jsp" /gt
  • Requests included file at runtime and returns
  • Dynamic forward
  • JSP forward tag ltjspforward page"file.jsp" /gt
  • Requests forwarded file at runtime and does not
    return
  • Extra functionality when dynamically loading a
    page
  • Send string parameters with jspparam tag
  • Use JSP expressions within the tags
  • Send object attributes to dynamically loaded page
  • Add attribute to the request object with
    request.setAttribute
  • Get attributes from the request object with
    request.getAttribute
Write a Comment
User Comments (0)
About PowerShow.com