Chapter 2: JavaServer Pages (JSP) Basics 1 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Chapter 2: JavaServer Pages (JSP) Basics 1

Description:

XML-compatible syntax (without using special characters): jsp:directive.taglib ... / 7 ... Static XML (.jspx file) Generates the static portion of the page. 8 ... – PowerPoint PPT presentation

Number of Views:403
Avg rating:3.0/5.0
Slides: 22
Provided by: conest
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2: JavaServer Pages (JSP) Basics 1


1
Chapter 2JavaServer Pages (JSP)Basics 1
Reference Beginning JSP
2
Objectives
  • Review of JSP
  • Quick Tour of JSP 2.0
  • Anatomy of a JSP page
  • JSP Directives, Actions
  • JSTL 1.1
  • JSP 2.0 EL
  • Including JSP pages
  • JSP Action and JavaBeans
  • Passing parameters to a JSP page

3
Java Server Pages - Review
  • Web scripting technology
  • Similar to Microsoft ASP and ASP.NET
  • Creates dynamic content using static templates
  • Java code enclosed in HTML
  • Combines markup (e.g., HTML) with Java Code to
    produce a dynamic Web Page
  • Java code runs on the server
  • Full power of the Java language (JDBC, EJB, etc.)
  • JSP tags allow non-Java developer to generate
    dynamic content

4
How JSPs Work - Review
JSP Request
Server reads JSP File
FileChanged ?
Yes
Translate to a Servlet
No
Compile Servlet
Execute Servlet
HTML Returned
5
Anatomy of a JSP page
Directive
  • lt_at_ taglib prefix"tags" tagdir"/WEB-INF/tags"
    gt
  • lt_at_ page language"java" import"" gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtJSP 2.0 Examples - Hello World Using a
    Tag Filelt/titlegt
  • lt/headgt
  • ltbodygt
  • lth1gtJSP 2.0 Examples - Hello World Using a Tag
    Filelt/h1gt
  • lthrgt
  • ltpgt lttagshelloWorld/gt.lt/pgt
  • ltH1gtWelcome back,
  • lt
  • String user (String) session.getAttribute("user
    ")
  • out.println(user)
  • gt
  • lt/H1gt
  • lt/bodygt
  • lt/htmlgt

Template Data
Action
Scripting Elements
6
JSP Directives
  • Reference page 32
  • Directives
  • Gives special instructions to the JSP container
  • Not used to generate output directly
  • Allowable directives (more details in Chapter 7)
  • page directive
  • taglib directive
  • include directive
  • Syntax lt_at_ directive gt
  • XML-compatible syntax (without using special
    characters)
  • ltjspdirective.taglib /gt

7
Template Data
  • Reference page 33
  • Template Data
  • Static text
  • Static HTML (.jsp file)
  • Static XML (.jspx file)
  • Generates the static portion of the page

8
JSP Actions
  • Reference page 34
  • Action elements
  • Directly involved in processing of request
  • Access data and manipulate data when generating
    dynamic output
  • Standard Action (more details in Chapter 8)
  • Available in every JSP 2.0 container
  • E.g., ltjspuseBeangt, ltjspgetPropertygt,
    ltjspsetPropertygt, ltjspincludegt
  • Custom Action
  • Actions created by developer

9
Scripting Elements
  • Reference page 35
  • Scripting Elements (more details in Chapter 3)
  • Embedded code, typically in Java
  • Directives
  • lt_at_ page language"java" import"" gt
  • Declarations
  • lt! java.text.SimpleDateFormat sdf new
    java.text.SimpleDateFormat("KKmm a' on 'MMMMM dd
    yyyy") gt
  • Scriptlets
  • lt String user (String) session.getAttribute("us
    er") gt
  • Expressions
  • lt sdf.format(new java.util.Date()) gt

10
HTML Form Submission
  • Reference
  • Page 42, 37
  • Using the GET method to submit a form
  • ltform action"showportal.jsp" method"get"gt
  • ltselect name"portchoice"gt
  • lt/formgt

11
GET vs. POST
  • Reference Page 37
  • GET
  • Attach parameters with the URL request
  • http//localhost8080/ch02/example1/showportal.jsp
    ?portchoicenews
  • Limitations
  • Length of parameter value cannot exceed max. URL
    length
  • Special characters must be encoded
  • POST
  • Uses HTTP header to send parameter information
  • Advantages
  • Length of parameter not limited
  • Less prone to tampering

12
JSTL 1.1 Example
  • Reference Page 42
  • showportal.jsp
  • lt_at_ taglib prefix"c" uri"http//java.sun.com/jsp
    /jstl/core" gt
  • lthtmlgt
  • ltcchoosegt
  • ltcwhen test"param.portchoice 'news'"gt
  • lt/cwhengt
  • lt/cchoosegt
  • lt/htmlgt

13
Working with JSTL 1.1
  • Reference Page 43
  • JSP Standard Tag Library (JSTL) 1.1
  • To perform common Web application programming
    tasks without using scripting elements
  • Use the taglib directive to use JSTL 1.1 tags
  • prefix prefix used for all tags in this library
  • uri URI to uniquely identify this tag library
  • JSP container finds the tag library (jstl.jar and
    standard.jar)
  • Contains many constructs
  • E.g., ltcchoosegt
  • Tomcat 5.x designed to work with JSTL 1.1, JSP
    2.0, Servlets 2.4

14
JSP 2.0 Expression Language
  • Reference Page 44
  • JSP 2.0 EL
  • Always evaluated at runtime
  • Always bracketed as
  • EL expression
  • Can be used
  • Anywhere template data may be placed
  • Within attributes of an action
  • E.g., test attribute of ltwhengt action
  • Can use EL implicit objects
  • param.portchoice 'news'

15
EL Implicit Objects
  • Reference Page 45
  • EL Implicit Objects (more details in Chapter 5)
  • pageContext, pageScope, requestScope
  • sessionScope, applicationScope
  • param, paramValues
  • header, headerValues
  • cookie, initParam

16
Including JSP pages
  • Reference Page 46
  • Individual portal pages reside in their own .jsp
    files.
  • showportal.jsp dynamically selects one of the
    portals pages (weather.jsp, news.jsp, etc.)
  • Use ltjspincludegt tag

17
JSP Best Practice
  • Reference Page 53
  • Start with simplest HTML prototype
  • Add style and layout later
  • Separate JSP development from HTML layout and
    formatting, if possible
  • Showportal.jsp (application logic) JSP developer
  • ltcwhen test"param.portchoice 'news'"gt
  • ltjspinclude page"news.jsp" /gt
  • News.jsp (layout and formatting) graphic
    designer

18
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

19
Multi-valued JavaBeans
  • Reference Pages 58 - 60
  • Uses the JSTL 1.1 iteration tag to create a table
    with multiple values
  • Instead of using ltjspgetPropertygt tag
  • Weather.jsp example
  • newsfeed.values returns a collection
  • city and temp are properties of row variable
  • lttablegt
  • ltcforEach items"newsfeed.values" var"row"
    gt
  • lttrgtlttd class"tableCell" width"200"gt
    row.city lt/tdgt
  • lttdgt row.templt/tdgt
  • lt/trgt
  • lt/cforEachgt
  • lt/tablegt

20
Passing parameters to a JSP page
  • Reference Page 67
  • Add a username parameter within ltFORMgt tag of
    index.jsp
  • In showportal.jsp, add a ltjspparamgt tag within a
    ltjspincludegt tag
  • Update portal web page (e.g., news.jsp) to
    display username
  • lttd class"boxTitle" gt
  • Welcome to the News Portal, param.user!
  • lt/tdgt

21
Next Steps
  • Try It Out sections of Textbook (Chapter 2)
  • example1, example2, example3
  • Assignment 1
Write a Comment
User Comments (0)
About PowerShow.com