chapter 61 processing the client request javaserver pages by xue bai

Post on 01-Apr-2015

229 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 6 1

Processing the Client Request

JavaServer Pages

By Xue Bai

Chapter 6 2

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

Chapter 6 3

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

Chapter 6 4

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

Chapter 6 5

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

Chapter 6 6

Header Name Description

Accept Specify 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-Agent Gives 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.

Referrer Gives 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

Chapter 6 7

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

Chapter 6 8

Header Information

Chapter 6 9

Determine Browser Type

<% String userAgent = request.getHeader("User-Agent"); %>

<% if(userAgent.indexOf("MSIE") != -1){ %>

You are using Internet Explorer

<% }else if(userAgent.indexOf("Netscape") != -1){ %>

You are using Netscape Navigator

<% }else{ %>

You are using a browser other than Netscape Navigator or Internet Explorer

<% } %>

Chapter 6 10

Example2.jsp Displayed in Internet Explorer

Chapter 6 11

Example2.jsp Displayed in Netscape Navigator

Chapter 6 12

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: <%= request.getRemoteHost() %>

Client computer IP address:

<%= request.getRemoteAddr() %>

The Web server name: <%= request.getServerName() %>

The running port number of Web server:

<%= request.getServerPort() %>

Chapter 6 13

Form Collection

• Use input elements to collect data:

– These input elements include text,

password, text area, hidden, select,

checkbox, and radio inputs

Chapter 6 14

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”>

Chapter 6 15

Password Field

• Characters are converted to asterisk or

bullet symbols for privacy

<input type=”password” name=”fieldname”>

Chapter 6 16

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

<input type=”hidden” name=”fieldName” value=”hidden data”>

Chapter 6 17

TextArea

• Accept multiple lines

<TextArea name=”fieldname”

rows=”numberOfRows” cols=”numberOfColumns”>

optional initial value

</TextArea>

• The rows and cols attributes are used to control the number of rows and columns displayed

Chapter 6 18

Select Fields

• Provide a list of options from which the user may select

• A drop down list box

<select name=”fieldname” size=n multiple>

<option value=”value” selected>Option1 text

…additional options

</select>

• 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

Chapter 6 19

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

Chapter 6 20

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

Chapter 6 21

Example 6.jsp

Gender:

<input type=radio name=gender value="Female">Female

<input type=radio name=gender value="Male">Male

<br><br>

What types of music do you listen to?<br>

<input type=checkbox name=music value=Rock>Rock

<input type=checkbox name=music value=Jazz>Jazz

<input type=checkbox name=music value=Classical>Classical

<input type=checkbox name=music value=Pop>Pop

Chapter 6 22

Working with Checkboxes and Radio Buttons

Chapter 6 23

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

Chapter 6 24

Processing Array

String music[] = request.getParameterValues(“music”);

if(music != null && music.length != 0){

out.println(“Music types you listen to: <br>”);

for(int i=0; i<music.length; i++){

out.println(music[i] + “<br>”);

}

}

Chapter 6 25

List All Control Names

• The method:

Request.getParameterNames()

• Returns an enumeration object containing

all control names on the request

Chapter 6 26

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

Chapter 6 27

Cookies

• A small piece of textual information

stored on the client machine

• A name and value pair

• Attributes include: maximumAge, domain, and path

Chapter 6 28

Out Object

• Used to send an output stream to the client

<%= “message” %>

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

Chapter 6 29

Flushing Buffered Content

• Force buffered content to be flushed,

regardless of the specified buffer size

out.flush();

Chapter 6 30

Clearing the Buffer

• Erase the buffered content

out.cler();

out.clearBuffer();

Chapter 6 31

Closing the Output Stream

• Close output stream

• Before closing output stream, the buffered content is flushed

out.close();

Chapter 6 32

Control Output Stream

<%@ page buffer="8kb" %>

<HTML><HEAD><TITLE>Explicitly close output stream</TITLE></HEAD>

<BODY>

Messages before closing output stream.<br>

Close output stream now.<br>

<%

out.close();

%>

output stream has been closed. So this content will

not be sent to client

</BODY>

</HTML>

Chapter 6 33

Closing the Output Stream

top related