AhmadLang / Java, How To Program, 2004
.pdf
JSPs should not store client state information in instance variables. Rather, they should use the JSP implicit session object. For more information on how to use the session object, visit Sun's J2EE tutorial at java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html.
Special characters or character sequences that the JSP container normally uses to delimit JSP code can be included in a JSP as literal characters in scripting elements, fixed template data and attribute values using escape sequences. Figure 27.3 shows the literal character or characters and the corresponding escape sequences and discusses where to use the escape sequences.
Figure 27.3. JSP escape sequences.
(This item is displayed on page 1288 in the print version)
|
Escape |
|
Literal |
sequence |
Description |
|
|
|
<% |
<\% |
The character sequence <% normally indicates the beginning of a |
|
|
scriptlet. The <\% escape sequence places the literal characters |
|
|
<% in the response to the client. |
%> |
%\> |
The character sequence %> normally indicates the end of a |
|
|
scriptlet. The %\> escape sequence places the literal characters |
|
|
%> in the response to the client. |
' |
\" |
As with string literals in a Java program, the escape sequences |
" |
\" |
for characters '," and \ allow these characters to appear in |
\ |
\\ |
attribute values. Remember that the literal text in a JSP |
|
|
becomes string literals in the servlet that represents the |
|
|
translated JSP. |
|
|
|
[Page 1288]
27.5.2. Scripting Example
Figure 27.4 demonstrates responding to get requests with basic scripting capabilities. The JSP enables the user to input a first name, then outputs that name in the response. Using scripting, the JSP determines whether a firstName parameter was passed as part of the request. If not, the JSP returns an XHTML document containing a form through which the user can input a first name. Otherwise, the JSP obtains the firstName value and uses it as part of an XHTML document that welcomes the user to JavaServer Pages.
Figure 27.4. Scripting a JavaServer Pagewelcome.jsp.
(This item is displayed on pages 1289 - 1290 in the print version)
1 |
<?xml version = "1.0" ?> |
||
2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
||
3 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
||
4 |
|
|
|
5 |
<!-- Fig. 27.4: welcome.jsp --> |
||
6 |
<!-- |
JSP that |
processes a "get" request containing data. --> |
7 |
|
|
|
8 |
<html |
xmlns = |
"http://www.w3.org/1999/xhtml"> |
9 |
|
|
|
10<!-- head section of document -->
11<head>
12<title>Processing "get" requests with data</title>
13</head>
14
15<!-- body section of document -->
16<body>
17<% // begin scriptlet
18
19 String name = request.getParameter( "firstName" );
20
21if ( name != null )
22{
23%> <%-- end scriptlet to insert fixed template data --%>
25 |
<h1> |
26 |
Hello <%= name %>, <br /> |
27 |
Welcome to JavaServer Pages! |
28 |
</h1> |
29 |
|
30 |
<% // continue scriptlet |
31 |
|
32} // end if
33else {
34 |
|
|
|
|
|
35 |
%> |
<%-- |
end scriptlet to insert |
fixed template data --%> |
|
36 |
|
|
|
|
|
37 |
|
|
<form action = "welcome.jsp" method = "get"> |
||
38 |
|
|
|
<p>Type your first name |
and press Submit</p> |
39 |
|
|
|
|
|
40 |
|
|
|
<p><input type = "text" name = "firstName" /> |
|
41 |
|
|
|
<input type = "submit" value = "Submit" /> |
|
42 |
|
|
|
</p> |
|
43 |
|
|
</form> |
|
|
44 |
|
|
|
|
|
45 |
<% |
// |
continue scriptlet |
|
|
46 |
|
|
|
|
|
47 |
|
} |
// |
end else |
|
48 |
|
|
|
|
|
49%> <%-- end scriptlet --%>
50</body>
51
52 </html> <!-- end XHTML document -->
[View full size image]
It is sometimes difficult to debug errors in a JSP, because the line numbers reported by a JSP container normally refer to the servlet that represents the translated JSP, not the original JSP line numbers. Program development environments enable JSPs to be compiled in the environment, so you can see syntax error messages. These messages include the statement in the servlet that represents the translated JSP, which can be helpful in determining the error.
Error-Prevention Tip 27.2
Many JSP containers store the source code for the servlets that represent the translated JSPs. For example, the Tomcat installation directory contains a subdirectory called work in which you can find the source code for the servlets translated by Tomcat. Recall from Chapter 26 that the log files located in the logs subdirectory of the Tomcat installation directory are also helpful for determining the errors.
Error-Prevention Tip 27.3
Always put the closing brace for the if statement and the else statement in the same scriptlet.
To test Fig. 27.4 in Tomcat, copy welcome.jsp into the jsp directory created in Section 27.3. Then, open your Web browser and enter the following URL:
http://localhost:8080/jhtp6/jsp/welcome.jsp
[Page 1291]
When you first execute the JSP, it displays the form in which you can enter your first name, because the preceding URL does not pass a firstName parameter to the JSP. After you submit your first name, your browser should appear as shown in the second screen capture of Fig. 27.4. [Note: As with servlets, it is possible to pass get request arguments as part of the URL. The following URL supplies the firstName parameter to welcome.jsp.]
http://localhost:8080/jhtp6/jsp/welcome.jsp?firstName=Paul
[Page 1291 (continued)]
27.6. Standard Actions
We continue our JSP discussion with the JSP standard actions (Fig. 27.5). These actions provide JSP implementors with access to several of the most common tasks performed in a JSP, such as including content from other resources, forwarding requests to other resources and interacting with JavaBean software components. JSP containers process actions at request time. Actions are delimited by
<jsp:action> and </jsp:action>, where action is the standard action name. In cases where nothing appears between the starting and ending tags, the XML empty element syntax <jsp: action /> can be used. Figure 27.5 summarizes the JSP standard actions, which we use in the next several subsections.
Figure 27.5. JSP standard actions.
Action |
Description |
|
|
<jsp:include> |
Dynamically includes another resource in a JSP. As the JSP |
|
executes, the referenced resource is included and processed. |
<jsp:forward> |
Forwards request processing to another JSP, servlet or static |
|
page. This action terminates the current JSP's execution. |
<jsp:plugin> |
Allows a plug-in component to be added to a page in the form |
|
of a browser-specific object or embed HTML element. In the |
|
case of a Java applet, this action enables the browser to |
|
download and install the Java Plug-in, if it is not already installed |
|
on the client computer. |
<jsp:param> |
Used with the include, forward and plugin actions to specify |
|
additional name-value pairs of information for use by these |
|
actions. |
JavaBean Manipulation |
|
<jsp:useBean> |
Specifies that the JSP uses a JavaBean instance (i.e., an object |
|
of the class that declares the JavaBean). This action specifies the |
|
scope of the object and assigns it an ID (i.e., a variable name) |
|
that scripting components can use to manipulate the bean. |
<jsp:setProperty> |
Sets a property in the specified JavaBean instance. A special |
|
feature of this action is automatic matching of request |
|
parameters to bean properties of the same name. |
<jsp:getProperty> |
Gets a property in the specified JavaBean instance and converts |
|
the result to a string for output in the response. |
|
|
[Page 1292]
27.6.1. <jsp:include> Action
JavaServer Pages support two include mechanismsthe <jsp:include> action and the include directive. Action <jsp:include> enables dynamic content to be included in a JavaServer Page at request time. If the included resource changes between requests, the next request to the JSP containing the <jsp:include> action includes the resource's new content. On the other hand, the include directive copies the content into the JSP once, at JSP translation time. If the included resource changes, the new content will not be reflected in the JSP that used the include directive, unless that JSP is recompiled, which normally would occur only if a new version of the JSP is installed. Figure 27.6 describes the attributes of action <jsp:include>.
1 |
<?xml version = "1.0"?> |
|
2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
|
3 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
4 |
|
|
5 |
<!-- |
Fig. 27.7: include.jsp --> |
6 |
|
|
7 |
<html |
xmlns = "http://www.w3.org/1999/xhtml"> |
8 |
|
|
9<head>
10<title>Using jsp:include</title>
12<style type = "text/css">
13body
14{
15 |
font-family: tahoma, helvetica, arial, sans-serif; |
16 |
} |
17 |
|
18table, tr, td
19{
20 |
font-size: . |
9em; |
21 |
border: 3px |
groove; |
22 |
padding: 5px; |
|
23 |
background-color: #dddddd; |
|
24}
25</style>
26</head>
27
28<body>
29<table>
30<tr>
31 |
<td style = |
"width: 160px; text-align: center"> |
32 |
<img src |
= "images/logotiny.png" |
33 |
width |
= "140" height = "93" |
34 |
alt = |
"Deitel & Associates, Inc. Logo" /> |
35 |
</td> |
|
36 |
<td> |
|
37 |
<%-- include banner.html in this JSP --%> |
|
38 |
<jsp:include page = "banner.html" |
|
39 |
flush |
= "true" /> |
40 |
</td> |
|
41</tr>
42<tr>
43 |
<td style = "width: 160px" > |
|
44 |
<%-- include |
toc.html in this JSP --%> |
45 |
<jsp:include |
page = "toc.html" flush = "true" /> |
46 |
</td> |
|
47 |
<td style = "vertical-align: top"> |
|
48 |
<%-- include |
clock2.jsp in this JSP --%> |
49 |
<jsp:include |
page = "clock2.jsp" |
50 |
flush = "true" /> |
|
51 |
</td> |
|
52</tr>
53</table>
54</body>
55</html>
[View full size image]
1 |
<!-- |
Fig. 27. |
9: |
toc.html |
|
--> |
2 |
<!-- |
contents |
to |
include |
in |
another document --> |
3 |
|
|
|
|
|
|
4<p><a href = "http://www.deitel.com/books/index.html">
5Publications/BookStore
6</a></p>
7
8<p><a href = "http://www.deitel.com/whatsnew.html">
9What's New
10</a></p>
11
12<p><a href = "http://www.deitel.com/books/downloads.html">
13Downloads/Resources
14</a></p>
15
16<p><a href = "http://www.deitel.com/faq/index.html">
17FAQ (Frequently Asked Questions)
18</a></p>
19
20<p><a href = "http://www.deitel.com/intro.html">
21Who we are
22</a></p>
23
24<p><a href = "http://www.deitel.com/index.html">
25Home Page
26</a></p>
27
28<p>Send questions or comments about this site to
29<a href = "mailto:deitel@deitel.com">
30deitel@deitel.com
31</a><br />
32Copyright 1995-2005 by Deitel & Associates, Inc.
33All Rights Reserved.
34</p>
Figure 27.10. JSP clock2.jsp to include as the main content in the XHTML document created by Fig. 27.7.
(This item is displayed on page 1296 in the print version)
1 |
<!-- |
Fig. |
27.10: clock2.jsp |
--> |
2 |
<!-- |
date |
and time to include in another document |
--> |
3 |
|
|
|
|
4<table>
5<tr>
6<td style = "background-color: black;">
7 |
<p |
class = "big" style = "color: cyan; font-size: 3em; |
|
8 |
|
font-weight: bold;"> |
|
9 |
|
|
|
10 |
|
<%-- script to determine client local and --%> |
|
11 |
|
<%-- format date accordingly |
--%> |
12 |
|
<% |
|
13 |
|
// get client locale |
|
14 |
|
java.util.Locale locale = request.getLocale(); |
|
15 |
|
|
|
16 |
|
// get DateFormat for client's Locale |
|
17 |
|
java.text.DateFormat dateFormat = |
|
18 |
|
java.text.DateFormat.getDateTimeInstance( |
|
19 |
|
java.text.DateFormat. LONG, |
|
20 |
|
java.text.DateFormat. LONG, |
locale ); |
21 |
|
|
|
22 |
|
%> <%-- end script --%> |
|
23 |
|
|
|
24 |
|
<%-- output date --%> |
|
25 |
|
<%= dateFormat.format( new java.util.Date() ) %> |
|
26 |
</p> |
|
|
27 |
</td> |
|
|
28 |
</tr> |
|
|
