Professional Documents
Culture Documents
JSP Aj
JSP Aj
. A/JSP expression tag begins with <%=, which is followed by the expression. ‘The JSP virtual engine resolves the JSP expression before sending the output of the JSP program to the browser. That is, the JSP tag <%=age%> is replaced with the number 29; afterwards, the HTML paragraph tag and related information is sent to the browser.
Your age is:
Your curved grade is: <=;
ae grade is:
Your curved grade is:
Your curved grade is: <¢=curve(70)%>
Control Statements One of the most powerful features available in JSP is the ability to change the flow of the program to truly create dynamic content for a web page based on conditions received from the browser. There are two control statements used to change the flow of a JSP program. These are the if statement and the switch statement, both of which are also used to direct the flow of a Java program. The if statement evaluates a condition statement to determine if one or more lines of code are to be executed or skipped (as you probably remember from when you learned Java). Similarly, a switch statement compares a value with one or more other values associated with a case statement. The code segment that is associated with the matching case statement is executed. Code segments associated with other case statements are ignored. : i The power of these controls comes from the fact that the code segment that is bination of HTML tags and JSP &xecuted or skipped can consist of HTML tags or a com ‘ags. That is, these code segments don’t need to be only Java statements or Java tags. : mts 11-6 shows how to intertwine HTML tags and JSP tags to ie the se ° JSP "I noti i m is confusing to read because Program. You'll notice that this progral TSP tags. This isn _ pattment and the switch statement are broken into several E use - ithi statements. a HTML tags are interspersed within these statement pepinning of theif The if stat ires three JSP tags. The firs tement, including the pies tee expression. ‘The second contains the else statement, 2d the third has the closed French brace used to terminate the else block.te Reference se6- J2EE: The Com tain Two HTML paragraph tags €o! . on the! evaluation of the conditional exp! information paragraph tage ae orfivided into several JSP tags because each case e switch st and related information. And, as with statement require an HTML para Op ond related information sociated the if statement, only that matches the switch value are returned to the browser. with a case statement ‘simple examples of the if statement and switch statement, You Listing 11-6 paler eataienial such as nesting if statements, by using techniques can create meres col es le. Any flow control statements that you use in Java can also illustrated Aa ee} rogram. However, you must be careful to separate JSP pebyeaee HTML aes Tri pieemation that will be executed when the program’s flow . ation that the browser displays, dependin iscion in the if statement. Only one of the HTM," are sent to the browser. changes. using 126 J amc> Using an WF Pi pgacyYou passed!
ae)Better luck next time.
Your final grade is a A
Your final grade is a B
Your final grade is ac
Your final grade is
is
entered into the program before the for loop begins. This is because the for loop is only
populating columns and not rows.
A pair of HTML table data cell tags tag and the close tag are inserted into the
Program following the French brace that closes the for loop block. These tags terminate
the construction of the table. ; d
A similar process is used to create the other two tables, except the while loop an
the do... while loop are used in place of the for loop.
Programs because loops are used
‘ are placed inside the for loop along with
aJSP tag that contains an element of the array. The JSP tag resolves to the value of the
array element by the JSP virtual program. , ;
The close table row
Seconds/TD>
<¥=Grade(x]8>
2
pe
First
Second
Third
<® x=0;
dof &>
stxtey
} while (x<3) &>Chapter 11: Java ServerPages
(J Tomeat
JSP programs are executed by a JSP Virtual Machine that runs on a web server.
Therefore, you'll need to have access to a JSP Virtual Machine to run your JSP program.
Alternatively, you can use an integrated development environment such as JBuilder
that has a built-in JSP Virtual Machine or you can download and install a JSP Virtual
Machine.
One of the most popular JSP Virtual Machines is Tomcat, and it is downloadable at
no charge from the Apache web site. Apache is also a popular web server that you can
also download at no cost .
You'll also need to have the Java Development Kit (DK) installed on your computer,
which you probably installed when you learned Java programming. You can download
the JDK at no charge from the wwwsun.com web site.
Here’s what you need to do to download and install Tomcat:
1. Connect to jakarta.apache.org.
2. Select Download.
3. Select Binaries to display the Binary Download page.
4. Create a folder from the root directory called tomcat.
5. Download the latest release of jakarta-tomcat.zip to the tomcat folder.
6. Unzip jakarta-tomcat.zip. You can download a demo co
PY of WinZip from
www.winzip.com if you don’t have a zip /unzip program installed on your
computer,
7. The extraction process should create the following folders in the tomcat
directory: bin, conf, doc, lib sre, and webapps.
8. Use a text editor such as Notepad and edit the JAVA_HOME variable in the
tomcat bat file, which is located in the \tomcat\bin folder. Make sure the
JAVA_HOME variable is assigned the path where the JDK is installed on
Your computer.
°. Open a DOS window and type \tomcat\bin\tomeat to start Tomcat.
10. Open your browser: Enter http’//localhost:8080. The Tomcat home page is
displayed on the screen verifying that Tomcat is running.
be
CO eee|) The Complete Reference
TT
| 3] Request String
i the Submit button is selecteg,
est string whenever : :
re tae eotating cone of the URLand the query string, as you learned at the
beginning of this chapter. Here’s a typical request string:
eo ="Bob"&lname=" Smiths
qf http: //waw.jimkeogh .com/jsp/myprogram. jsp?fname="Bob"&1name="Snith
i tract values of fields that are to
Your program needs to parse the query string to extract f
be roca voy your program. You can parse the query string by using methods of the
JSP request object. Boa
} The getParameter(Name) is the method used to parse a value of a specific field. The
getParameter() method requires an argument, which is the name of the field whose
ue you want to retrieve. 5
=e ‘Lets say that you want to retrieve the value of the fname field and the value of the
Iname field in the previous request string. Here are the statements that you'll need in
your JSP program:
<%! String Firstname = request .getParameter (fname) ;
String Lastname = request .getParameter (1name) ;
eS
In the previous example, the first statement used the getParameter() method to copy
the value of the fname from the request string and assign that value to the Firstname
object. Likewise, the second statement performs a similar function, but using the value
of the Iname from the request string. You can use request string values throughout your
program once the values are assigned to variables in your JSP program.
There are four predefined implicit objects that are in every JSP program. These are
request, response, session, and out. The previous example used the request object's
getParameter() method to retrieve elements of the request string. The request objects
an instance of the HttpServletRequest (see Chapter 9). The response object is an instance
of HttpServletResponse, and the session object is an instance of HttpSession. Both of
these are described in detail in Chapter 9, The out object is an instance of the JspWritet
that is used to send a response to the client,
Copying a value from a multivalued field such as a selection lis field can be trickY
since there are multiple instances of the field ith a di Howevet,
you can easily handle multival le name, each with a different value.
lued fields by using the getParameterValues() method:
The getParameterValues() method is desiene’ toe eter’
the field specified as the ae method is designed to return multiple values from
iment to the getParamet is how the
getParameterValues() is implemented in ¢ JSP Poa HereChapter 11: Jaya ServerPages
318 M <#! String ( ) BATE = request. getPar
ont EMAIL (0]%>
are EMAIL [1]%>
ameterValues (*EMATLADDRESS “) ; >
You can parse field names by using the getParameterNames! i
returns an enumeration of String objets hat contains the ld nae the soca
string. You can use the enumeration extracting methods that you leamed in Jave to
copy field names to variables within your program.
‘The Quick Reference Guide in Chapter 9 contains a list of other commonly used
HitpServletRequest class methods that you'll find useful when working with parameters.
Parsing Other Information
The request string sent to the JSP by the browser is divided into two general components
that are separated by the question mark. The URL component appears to the left of the
question mark and the query string is to the right of the question mark.
In the previous section you learned how to parse components of the query string,
which are field names and values using request object methods. These are similar to
the method used to parse URL information.
The URLis divided into four parts, beginning with the protocol. The protocol defines
the rules that are used to transfer the request string from the browser to the JSP program.
Three of the more commonly used protocols ES HTTP, HTTPS (the secured version of
isa file transfer protocol
RNeuesnraa port combination. The host is the Internet Protocol (IP) address
or name of the server that contains the JSP program. The port number is the port pe :
the host monitors. Usually the port is excluded from the request string vebenevar
is used because the assumption is the host is monitoring port 80. Fal eg ce
and port is the virtual path of the JSP program. The server maps the vi Pi
physical path. ‘ A on
Heres a typical URL. The http is the protocol. The host i bes scrreai ie at
There isn’t a port because the browser assumes that the serv‘
The virtual path is /jsp/myprogram jsp.
r
rs
£
2
5
FE
3
Es
jsp
I http://www. j imkeogh.com/3sp/myprogran:You might also like