Introduction to JSP with Forms and JavaBeans - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Introduction to JSP with Forms and JavaBeans

Description:

JSP hides these GET/POST details (see request.getParameter and jsp:setProperty ... Define properties (fields) Define get/set methods. See following example ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 33
Provided by: courses2
Category:

less

Transcript and Presenter's Notes

Title: Introduction to JSP with Forms and JavaBeans


1
Introduction to JSPwith Forms and JavaBeans
  • Simon M. Lucas
  • CC292

2
Overview
  • Embeds Java code
  • In HTML tags
  • When used well
  • Simple way to generate dynamic web-pages
  • When misused (complex embedded Java)
  • Terribly messy (and may violate OaOO)
  • Keep the embedded Java simple
  • Use external helper classes (Beans?)

3
Life Cycle
  • A JSP page is translated into a Java Servlet
  • And then compiled
  • On Tomcat, the compilation happens the first time
    a page is requested
  • First request can be very slow!
  • Afterwards, just as fast as a Servlet (because it
    is then a servlet)

4
Hello World
  • lthtmlgt
  • ltheadgt lttitlegt Hello JSP lt/titlegt lt/headgt
  • ltbodygt
  • ltpgt Hello World
  • lt new java.util.Date() gt
  • lt/pgt
  • lt/bodygt
  • lt/htmlgt
  • See also Date_jsp.java the Servlet this page
    is translated to

5
Date_jsp.java (extract)
  • This extract shows the part that produces the
    output compare it with the JSP
  • out pageContext.getOut()
  • _jspx_out out
  • out.write("lthtmlgt\r\n")
  • out.write("ltheadgt ")
  • out.write("lttitlegt Hello JSP ")
  • out.write("lt/titlegt ")
  • out.write("lt/headgt\r\n")
  • out.write("ltbodygt \r\n")
  • out.write("ltpgt Hello World\r\n ")
  • out.print( new java.util.Date() )
  • out.write("\r\n")
  • out.write("lt/pgt\r\n")
  • out.write("lt/bodygt\r\n")
  • out.write("lt/htmlgt\r\n")

6
Produced
7
Basic Constructs
  • So far weve seen literals
  • E.g. lthtmlgt
  • Copied straight to output (to browser)
  • And expressions
  • E.g. lt new java.util.Date() gt
  • Return a value included in the output
  • Also have
  • Directives, Declarations and Scriptlets

8
Directives
  • Instructions to the compiler
  • Examples
  • Include another page (compile-time)
  • lt_at_ include file"header.jsp" gt
  • Import some Java packages (comma sep.)
  • lt_at_ page importjava.util.Collectiongt

9
Declarations
  • Used to declare variables and methods
  • Go in the declaration section of the Servlet
  • Can then be used later in the JSP page
  • Note the syntax
  • Examples
  • lt! int count 0 gt
  • lt! double sqr(double x) return x x
    gt

10
Scriptlets
  • These are sections of Java code embedded in the
    page
  • Unlike expressions, they do not return a value
  • But may write directly to the page
  • Get the writer response.getWriter()
  • They go in the service method of the servlet
  • Get executed each time a page is requested

11
Illustrative Example
  • Demonstrates much of the above
  • lt! int n 0 gt
  • Page accessed lt n gt times
  • lt if ( (n 10) 0 )
  • n 0
  • gt

12
What happens on next refresh?
13
Request and Response
  • Each JSP page has access to two special objects
  • The Request object carries information passed by
    the HTTP request (e.g. made by the browser)
  • This includes any submitted form data
  • The Response object is used to pass information
    back to the Client (browser)
  • E.g. response.getWriter() provides an output
    stream for direct writing to the client

14
Form Handling with JSP
  • JSP makes form handling easy
  • Can use request.getParameter() to get submitted
    values
  • Or can define a JavaBean to grab the values
    semi-automatically.
  • Well see this in action later with a simple
    example

15
HTML Forms
  • Allow user to supply input
  • Text Fields single line
  • Password field single line, blanked-out text
  • Text Areas multi-line
  • Choice (pop-up menu)
  • Radio-button (1 from n)
  • Check-box (m from n)
  • Browse button
  • Submit buttons

16
Form Generation
  • Need to generate the HTML to send to the client
  • The forms should be well presented
  • e.g. aligned in a table
  • Need to name the input fields
  • So that we may extract the data from the
    submitted form
  • Next some sample form elements

17
Some sample form elements
  • linked/FormElements.html

18
Form Processing
  • The form can specify whether data is supplied via
    a GET or POST request
  • POST is the usual way
  • Therefore, a servlet should implement the
    doPost() method to process a form
  • JSP hides these GET/POST details (see
    request.getParameter and ltjspsetPropertygt)

19
Submission Data Formats
  • When a form is generated, it can also specify the
    data format
  • By default, this is plain text
  • But can also be a MIME format
  • MIME allows upload of binary data (via base-64
    encoding)
  • Hence allows files of any type to be uploaded to
    a web server

20
Form Life-Cycles and Data Models
  • Where does a form come from?
  • Where are the data-types specified?
  • Whats the destination for the form input data?
  • How do we best validate it on the server?
  • What kind of data models are appropriate?
  • Can we separate form content from form
    presentation?

21
Form Processing Architectures
  • Helper classes for servlets
  • To generate forms ( other HTML)
  • Process the form input
  • JSP JavaBeans (more later)
  • JSP Tag Library (not covered in this course)
  • XForms not covered, but well worth a read if
    youre keen!
  • MS InfoPath installed in the Lab can
    auto-generate forms from XML Schemas

22
JavaBeans
  • Come in two types
  • Simple (this course)
  • Enterprise (EJB more complex, not covered)
  • Simple JavaBeans
  • Data bound classes
  • Define properties (fields)
  • Define get/set methods
  • See following example

23
Hotel Booking Form
24
HTML (body) for Booking Form
  • ltbodygt
  • lth3 align"center"gtWelcome to the Hotel
    Californialt/h3gt
  • ltform method"POST" action"BookHotel.jsp"gt
  • ltpgtName ltinput type"text" name"name"
    size"20"gtlt/pgt
  • ltpgtHow many nights
  • ltselect size"1" name"nNights"gt
  • ltoption selected""gt1lt/optiongt
  • ltoptiongt2lt/optiongt
  • ltoptiongt3lt/optiongt
  • lt/selectgtlt/pgt
  • ltpgt
  • ltinput type"submit" value"Submit" name"B1"gt
  • lt/pgt
  • lt/formgt
  • lt/bodygt

25
Accessing Submitted Values(manual version)
  • lthtmlgt
  • ltheadgtlttitlegtBean testlt/titlegtlt/headgt
  • ltbodygt
  • lth2gt
  • ltrequest.getParameter("name") gt
  • to stay for
  • lt request.getParameter("nNights") gt nights.
  • lt/h2gt
  • lt/bodygt
  • lt/htmlgt

26
JavaBean Version (Auto)
  • ltjspuseBean id'roomBooking'
  • scope'page'
  • class'beans.HotelBean'
  • /gt
  • ltjspsetProperty name'roomBooking' property''
    /gt
  • lthtmlgt
  • ltheadgtlttitlegtBean testlt/titlegtlt/headgt
  • ltbodygt
  • lth2gt ltroomBooking.getName()gt
  • to stay for
  • lt roomBooking.getnNights() gt nights.
    lt/h2gt
  • lt/bodygt
  • lt/htmlgt

27
Java Code for Bean Version
  • package beans
  • public class HotelBean
  • String name
  • int nNights
  • public String getName()
  • return name
  • public void setName(String name)
  • this.name name
  • public int getnNights()
  • return nNights
  • public void setnNights(int nNights)
  • this.nNights nNights

28
Resulting Page
29
Bean Scope
  • Note the setting scope'page'
  • Scope has four possible settings
  • Page
  • Bean exists for execution of that page only
  • Request
  • Like page, but survives any forward or include
    requests

30
Bean Scope (cont.)
  • Session
  • The Bean exists for multiple requests within the
    web application, from a particular web browser
    instance
  • Used for shopping baskets etc.
  • Application
  • The Bean exists for all requests from all users,
    for all pages that use it.
  • Survives until the Web Application Server is
    restarted
  • Can be used for a database connection (or
    connection pool)

31
Beans v. Manual
  • For this example, consider the differences
  • JavaBean JSP page more complex
  • JavaBean also required an external class
    definition
  • Discussion question
  • When would JavaBean solutions be better than
    manual versions?
  • Answer

32
Lab Exercises
  • Try typing in and running running these JSP
    examples for yourself
  • Instructions for Tomcat see Lab pages
  • Also study the servlet code produced (in the
    tomcat\work directory
Write a Comment
User Comments (0)
About PowerShow.com