CSCI 6962: Serverside Design and Programming - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

CSCI 6962: Serverside Design and Programming

Description:

Prevents bookmarking (good if part of session that user should no be able to enter in middle) Allows bookmarking of pages. Parameters limited to ~4k of data ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 24
Provided by: csis8
Category:

less

Transcript and Presenter's Notes

Title: CSCI 6962: Serverside Design and Programming


1
CSCI 6962 Server-side Design and Programming
  • Lecture 3 Introduction to Java Server Pages

2
Form Handling
  • Form data appended to request string
  • Generates the request
  • http//frodo.cis.ysu.edu/john/cgi-bin/test.plqua
    ntity3

ltFORM NAME"purchaseform" METHODGET
ACTIONhttp//frodo.cis.ysu.edu/john/cgi-bin/test
.plgt Quantity ltINPUT TYPE"text" SIZE"8"
NAME"quantity" /gt ltBR /gtltBR /gt ltINPUT
TYPE"submit" VALUE"SUBMIT"gt lt/FORMgt
3
Form Handling
  • Goal
  • Extract values of parameters from the request
  • Generate appropriate response page based on
    parameter values
  • Take other appropriate action based on request
  • Add to shopping cart
  • Modify or query database

4
Server Page Model
  • Html document with executable code interspersed
  • When page requested
  • Code executed
  • Html generated and inserted in its place
  • Final all html document sent back as response

Tomcat server
request for somepage.jsp
somepage.jsp html html Java html Java html html
html html Java html html
resulting html page html html html html html
html html html html html html html
5
Java Server Pages
6
JSP Syntax
  • Basic tag form lt gt
  • Simplest formlt some Java expression gt
  • Tomcat evaluates expression to get value
  • Inserts that value in place of expression in
    generated html page

7
JSP Simple Example
  • Simple example

Java Server Page
2 2 evaluated to value of 4
Resulting html Page
8
JSP Syntax
  • Basic tag form lt gt
  • Executes code inside brackets without generating
    html
  • Set variables later used to generate html later
  • Store/access values in session/databases

9
JSP Syntax
Stores value of 4 in sum variable
Value of 4 in sum used in this JSP
No html here
10
JSP Scoping
  • Variable declared in a block of JSP on a page
  • May be accessed by any other JSP on same page
  • No access to variable from any other page
    (purpose of sessions)

11
Html Forms
  • The FORM tagltform actionurl of response page
    methodget or postgtlt/formgt

Method by which form data is passed
Relative url if response page is part of same
webapp
12
Get vs. Post Method
13
Simple Form Elements
  • TEXT tagltinput typetext nameelementname/gt
  • SUBMIT tagltinput typesubmit
    valuebuttonlabel/gt

Necessary for value to be sent to server
14
Form Element Example
  • ltform action"orderReply.jsp" method"get"gt
  • lttable cellspacing"5"gt
  • lttrgt
  • lttd align"right"gtNumber to purchaselt/tdgt
  • lttdgtltinput type"text" name"quantity"gtlt/tdgt
  • lt/trgtlttrgt
  • lttd align"right"gtYour namelt/tdgt
  • lttdgt ltinput type"text" name"customerName"gt
    lt/tdgt
  • lt/trgtlttrgt
  • lttd align"right"gtYour emaillt/tdgt
  • lttdgt ltinput type"text" name"customerEmail"
    gtlt/tdgt
  • lt/trgtlttrgt
  • lttdgtlt/tdgt
  • lttdgtltinput type"submit" value"Place
    Order"gtlt/tdgt
  • lt/trgt
  • lt/tablegt
  • ltformgt

15
Form Element Example
16
Form Parameter Passing
Parameter string passedquantity137customerName
JohnSullinscustomerEmailjohn_at_cis.ysu.edu
17
Handling Form Data
  • request object in JSP
  • Java object created from request string
  • Contains request data and methods to easily
    access that data
  • Accessed by JSP code

request
Data from form Other data about request
methods to access data about the request
Code in JSP
18
Handling Form Data
  • Most useful methodString request.getParameter(St
    ring)
  • Example String name
    request.getParameter("customerName") sets the
    value of name to John Sullins

Takes name of form element as parameter
Returns the corresponding value passed to the
server
19
Example JSP
  • ltbodygt
  • lt
  • String name request.getParameter("customerNa
    me")
  • String email request.getParameter("customerE
    mail")
  • String quantity request.getParameter("quanti
    ty")
  • gt
  • lth2gtOrder Confirmationlt/h2gt
  • ltpgt
  • Thank you for your order of lt quantity gt
    widgets, lt name gt.
  • lt/pgt
  • ltpgt
  • You will shortly receive an email
    confirmation at lt email gt.
  • lt/pgt
  • lt/bodygt

20
Acquiring Form Data
  • Statements to get and store form data lt
  • String name request.getParameter("cust
    omerName")
  • String email request.getParameter("cust
    omerEmail")
  • String quantity request.getParameter("q
    uantity")
  • gt

21
Displaying Values in Response
  • ltpgt
  • Thank you for your order of lt quantity gt
    widgets, lt name gt.
  • lt/pgtltpgt
  • You will shortly recieve an email confirmation
    at lt email gt.
  • lt/pgt

137
John Sullins
john_at_cis.ysu.edu
22
Commenting JSP Files
  • Crucial to future maintenance of site
  • Inside of JSP code (between lt and gt)//
    comment/ comment /
  • Outside of JSP code (that is, in html)lt!--
    comment --gt

23
Importing Library Classes
  • Much of Java classes in separate libraries
  • Must be imported to be used in JSP
  • Syntaxlt_at_ page importlist of Java classes gt
  • Examplelt_at_ page importjava.util.Date,
    java.io. gt

Date class in the util library
All classes in the io library
Write a Comment
User Comments (0)
About PowerShow.com