Download presentation
Presentation is loading. Please wait.
Published byBrenda Bissett Modified over 10 years ago
1
Chapter 61 Processing the Client Request JavaServer Pages By Xue Bai
2
Chapter 62 Objectives In this chapter, you will: Obtain header information using the request object Get client and server information using the request object Process form input fields Get and use array containing all values of a single input field Control output stream
3
Chapter 63 The Implicit Objects There are a number of objects made available by the JSP container These objects are called implicit objects, because you can use them without explicitly declaring them in your page These objects are instances of classes defined by the servlet and JSP specifications Request, response, out, session, application, and exception
4
Chapter 64 Request object Each time you request a JSPpage, the JSP container creates a new instance of the request object The object contains information about the request and the invoked page, including headers, client and server information, request URL, cookies, session, and input data
5
Chapter 65 Header Information HTTP requests can have a number of associated HTTP headers These headers provide some extra information about the request You can use the header information to customize the content you send to client
6
Chapter 66 Header Name Description AcceptSpecify the media type, Multipurpose Internet Multimedia Extensions (MIME), the client prefers to accept. All media types are separated by commas. For example, the image format a browser prefers. User-AgentGives information about the client software, including the browser name and version as well as information about the client computer. For example, Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1) indicates it is Microsoft Internet Explorer running on Windows XP. ReferrerGives the URL of the document that refers to the requested URL (that is, the document that contains the link the client followed to access this document). Accept- Language The default language setting on the client machine
7
Chapter 67 Get Header Information –It returns a string containing information about the header passed as a parameter request.getHeader(“header name”); Example: –The string returned by request.getHeader(“accept- language”) indicates the default language setting on the client machine The method request.getHeaderNames() returns an enumeration of all header names on the current request
8
Chapter 68 Header Information
9
Chapter 69 Determine Browser Type You are using Internet Explorer You are using Netscape Navigator You are using a browser other than Netscape Navigator or Internet Explorer
10
Chapter 610 Example2.jsp Displayed in Internet Explorer
11
Chapter 611 Example2.jsp Displayed in Netscape Navigator
12
Chapter 612 Get Client and Server Information The request object provides various methods to get client computer name and IP, Web server name, and Web server port Client computer name: Client computer IP address: The Web server name: The running port number of Web server:
13
Chapter 613 Form Collection Use input elements to collect data: –These input elements include text, password, text area, hidden, select, checkbox, and radio inputs
14
Chapter 614 Text Field Accept single-line information –maxlength—Sets the maximum allowable length of the field, in characters –size—Sets the width, in characters, of the input box that appears on the page –value—Sets initial value for the text field <input type=”text” name=”state” size=”4” maxlength=”2” value=”VA”>
15
Chapter 615 Password Field Characters are converted to asterisk or bullet symbols for privacy
16
Chapter 616 Hidden Field Another type of txt field, but they are not displayed Used to send data to the Web server that cannot be changed by users Often used to pass data from one page to another
17
Chapter 617 TextArea Accept multiple lines <TextArea name=”fieldname” rows=”numberOfRows” cols=”numberOfColumns”> optional initial value The rows and cols attributes are used to control the number of rows and columns displayed
18
Chapter 618 Select Fields Provide a list of options from which the user may select A drop down list box Option1 text …additional options The size attribute controls how many lines are visible at one time The multiple attribute controls whether users may select multiple items from the list
19
Chapter 619 Checkboxes A small box that users click to place or remove a check mark <input type=checkbox name=”fieldname” value=”a value” CHECKED>Descriptive text
20
Chapter 620 Radio Buttons Present a range of choices Only one button in a group is selected at one time <input type=radio name=”fieldname” value=”field value” checked>Descriptive text
21
Chapter 621 Example 6.jsp Gender: Female Male What types of music do you listen to? Rock Jazz Classical Pop
22
Chapter 622 Working with Checkboxes and Radio Buttons
23
Chapter 623 Working with Array Deal with multiple values associated with a single field on a form The method request.getParameterValues (“elementName”) returns a string array containing all values associated with the element
24
Chapter 624 Processing Array String music[] = request.getParameterValues(“music”); if(music != null && music.length != 0){ out.println(“Music types you listen to: ”); for(int i=0; i<music.length; i++){ out.println(music[i] + “ ”); }
25
Chapter 625 List All Control Names The method: Request.getParameterNames() Returns an enumeration object containing all control names on the request
26
Chapter 626 Response Object The response object controls output that the server sends to the client It is an instance of the javax.servlet.http.ServletResponse class (which is imported automatically when your JSP page is converted into a servlet) The response object provides methods that you can use in your JSP script to set headers, add cookies, and control how information is returned to clients Various methods of the response object are discussed throughout the book when they are introduced
27
Chapter 627 Cookies A small piece of textual information stored on the client machine A name and value pair Attributes include: maximumAge, domain, and path
28
Chapter 628 Out Object Used to send an output stream to the client
29
Chapter 629 Flushing Buffered Content Force buffered content to be flushed, regardless of the specified buffer size out.flush();
30
Chapter 630 Clearing the Buffer Erase the buffered content out.cler(); out.clearBuffer();
31
Chapter 631 Closing the Output Stream Close output stream Before closing output stream, the buffered content is flushed out.close();
32
Chapter 632 Control Output Stream Explicitly close output stream Messages before closing output stream. Close output stream now. <% out.close(); %> output stream has been closed. So this content will not be sent to client
33
Chapter 633 Closing the Output Stream
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.