chapter 31 basic form-processing techniques javaserver pages by xue bai

27
Chapter 3 1 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Upload: lee-morgan

Post on 16-Dec-2015

224 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 1

Basic Form-Processing Techniques

JavaServer Pages

By Xue Bai

Page 2: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 2

Objectives

In this chapter, you will:

• Design a form to collect information from users

• Use the GET method to send form information to the server

• Use the POST method to send form information to the server

• Retrieve information from a form

• Output information to users

• Use variables to store information obtained from a form

Page 3: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 3

Collecting Information

• A form serves as a container that holds controls such as text fields, labels, buttons, and images

• Form tags:

<FORM NAME="formName" ACTION="getUserName.jsp" METHOD="POST">

<!--form elements go inside of form tags -->

</FORM>

Page 4: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 4

Form Attributes

• Name:

– is required if you want form controls processed on the client side; otherwise, it is optional

• Action:

– Specify the destination Web page where the form is processed

• METHOD:

– Specify how to send form to the Web server, either POST or GET

Page 5: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 5

Control Element in a Form

• The input fields on a form are called controls and are usually (but not always) created with the <INPUT> tag

<FORM NAME="frmName" ACTION="getUserName.jsp" METHOD="POST">

Your Last Name:<INPUT TYPE = "TEXT" NAME="lastName">

</FORM>

Page 6: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 6

Control Elements’ Attributes

• TYPE:

– Specifies the input type

• NAME:

– Each control in a form must have a NAME. The data entered by the user is retrieved by referencing the name of its control field. Without a name, the data stored in the control field cannot be retrieved

• VALUE:

– A default value may be assigned to a control element

Page 7: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 7

For Elements Example

<FORM NAME="frmName" ACTION="getUserName.jsp" METHOD="POST">

Your First Name:<INPUT TYPE = "TEXT" NAME="firstName"><br>

Your Middle Name:<INPUT TYPE = "TEXT" NAME="middleName"><br>

Your Last Name:<INPUT TYPE = "TEXT" NAME="lastName">

</FORM>

Page 8: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 8

Form Example

Page 9: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 9

A Text Input with Default Value

<FORM >

<INPUT TYPE = "TEXT" NAME="txtField"

VALUE="This is the default text">

</FORM>

Page 10: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 10

Submitting Form for Processing

• In order to process form data, you need to submit the form to the server, and then retrieve and process data on the server-side

• To submit a form, the submit() method of the form must be called

– Using Submit button

– Explicitly call the submit method of a form

Page 11: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 11

Using Submit Button

<FORM NAME="frmName" ACTION="getUserName.jsp" METHOD="POST">

Your First Name:<INPUT TYPE = "TEXT" NAME="firstName"><br>

Your Middle Name:<INPUT TYPE = "TEXT" NAME="middleName"><br>

Your Last Name:<INPUT TYPE = "TEXT" NAME="lastName"><br>

<INPUT TYPE=SUBMIT VALUE="Submit">

<INPUT TYPE=RESET VALUE="Reset">

</FORM>

Page 12: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 12

Submit Button

Page 13: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 13

Explicitly Call the Submit Method of a Form

<FORM NAME=“balance” ACTION=“processBalance.jsp”>

Balance:<INPUT TYPE=TEXT NAME=amt>

<a href=“JavaScript:document.balance.submit()”>

Submit the form

</FORM>

Page 14: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 14

GET Method

• Form data is appended to the end of the designated URL after a question mark

• The URL is the one specified as the ACTION value in a form

• If you do not explicitly set the METHOD attribute for a form, by default the form is sent using the GET method

Page 15: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 15

GET Method and Query String

<FORM NAME="frmName" ACTION="getUserName.jsp" METHOD="GET">

Your First Name:<INPUT TYPE = "TEXT" NAME="lastName"><br>

Your Middle Name:<INPUT TYPE = "TEXT" NAME="lastName"><br>

Your Last Name:<INPUT TYPE = "TEXT" NAME="lastName"><br>

<INPUT TYPE=SUBMIT VALUE="Submit">

<INPUT TYPE=RESET VALUE="Reset">

</FORM>

Page 16: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 16

Submit a Form with the GET Method

Page 17: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 17

Get User Information

Page 18: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 18

POST Method

• When you use the POST method, the form element data is sent to the server as a transaction message body

• Unlike the GET method, the POST method does not append form element data to the URL

• Note that when the POST method is used, you can generate a query string by attaching a queryString to the URL directly

Page 19: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 19

Retrieve Query String

<%= request.getQueryString() %>

Page 20: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 20

Retrieve Data Stored in a Control Element

request.getParameter(“elementName”)

Example:

request.getParameter(“major”);

request.getParameter(“balance”);

Page 21: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 21

Form Processing Techniques

Page 22: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 22

Retrieve Form Data

<BODY BGCOLOR="<%= request.getParameter("bgColor")%>">

Hello, <%= request.getParameter("myName") %>:

here is your message displayed using your preferences:<br>

<br>

<font color="<%= request.getParameter("fontColor")%>"

size="<%= request.getParameter("fontSize") %>">

<%= request.getParameter("message")%>

</font>

</BODY>

Page 23: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 23

Output to the user

<%= “Your message goes here” %>

<% out.println(“Your message goes here”); %>

Unlike the first output method: <%= %>, with the out.print method, you can put many statements within a single pair of <% %>

Page 24: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 24

Storing Form Information

• There are many situations in which you will need to store form information and use it later in your JSP scripts. Like all programming languages, JSP uses variables to temporarily store information

• A variable is a location in computer memory where a value is stored for use by a program

• In JSP script, all variables must be declared before you can use them

• After a variable has been declared, a value can be stored in the variable, and this value can be used throughout your JSP page

Page 25: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 25

Using Variables

<% String major = request.getParameter(“major”);

out.println(major);

%>

<%= major %>

Page 26: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 26

Using Basic JSP Techniques

Page 27: Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai

Chapter 3 27

Using Basic JSP Techniques