Title: CSCI 6962: Serverside Design and Programming
1CSCI 6962 Server-side Design and Programming
- Lecture 3 Introduction to Java Server Pages
2Form 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
3Form 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
4Server 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
5Java Server Pages
6JSP 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
7JSP Simple Example
Java Server Page
2 2 evaluated to value of 4
Resulting html Page
8JSP 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
9JSP Syntax
Stores value of 4 in sum variable
Value of 4 in sum used in this JSP
No html here
10JSP 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)
11Html 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
12Get vs. Post Method
13Simple Form Elements
- TEXT tagltinput typetext nameelementname/gt
- SUBMIT tagltinput typesubmit
valuebuttonlabel/gt
Necessary for value to be sent to server
14Form 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
15Form Element Example
16Form Parameter Passing
Parameter string passedquantity137customerName
JohnSullinscustomerEmailjohn_at_cis.ysu.edu
17Handling 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
18Handling 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
19Example 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
20Acquiring 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
21Displaying 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
22Commenting 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
23Importing 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