Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai.

Similar presentations


Presentation on theme: "Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai."— Presentation transcript:

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

2 Chapter 32 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

3 Chapter 33 Collecting Information A form serves as a container that holds controls such as text fields, labels, buttons, and images Form tags:

4 Chapter 34 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

5 Chapter 35 Control Element in a Form The input fields on a form are called controls and are usually (but not always) created with the tag Your Last Name:

6 Chapter 36 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

7 Chapter 37 For Elements Example Your First Name: Your Middle Name: Your Last Name:

8 Chapter 38 Form Example

9 Chapter 39 A Text Input with Default Value

10 Chapter 310 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

11 Chapter 311 Using Submit Button Your First Name: Your Middle Name: Your Last Name:

12 Chapter 312 Submit Button

13 Chapter 313 Explicitly Call the Submit Method of a Form Balance: Submit the form

14 Chapter 314 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

15 Chapter 315 GET Method and Query String Your First Name: Your Middle Name: Your Last Name:

16 Chapter 316 Submit a Form with the GET Method

17 Chapter 317 Get User Information

18 Chapter 318 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

19 Chapter 319 Retrieve Query String

20 Chapter 320 Retrieve Data Stored in a Control Element request.getParameter(“elementName”) Example: request.getParameter(“major”); request.getParameter(“balance”);

21 Chapter 321 Form Processing Techniques

22 Chapter 322 Retrieve Form Data "> Hello, : here is your message displayed using your preferences: " size=" ">

23 Chapter 323 Output to the user Unlike the first output method:, with the out.print method, you can put many statements within a single pair of

24 Chapter 324 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

25 Chapter 325 Using Variables <% String major = request.getParameter(“major”); out.println(major); %>

26 Chapter 326 Using Basic JSP Techniques

27 Chapter 327 Using Basic JSP Techniques


Download ppt "Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai."

Similar presentations


Ads by Google