AhmadLang / Java, How To Program, 2004
.pdf
[Page 1323 (continued)]
Terminology
%\> escape sequence for %>
<!-- and --> XHTML comment delimiters
<%-- and --%> JSP comment delimiters
<% and %> scriptlet delimiters
<%! and %> declaration delimiters
<%= and %> JSP expression delimiters
<%@ and %> directive delimiters
<\% escape sequence for <% ${exp}
acceptChanges method of CachedRowSet autoFlush attribute of page directive beanName attribute of <jsp:useBean> action beforeFirst method of CachedRowSet buffer attribute of page directive
class attribute of <jsp:useBean> action comment
config implicit object
contentType attribute of page directive directive
dynamic content
[Page 1324]
error page
errorPage attribute of page directive
execute method of CachedRowSet
expression
Expression Language
extends attribute of page directive file attribute of include directive fixed-template data
fixed-template text
flush attribute of <jsp:include> action forward a request
getParameterValues method of request object
id attribute of <jsp:useBean> action implicit object
implicit object scopes
import attribute of page directive include a resource
include directive
info attribute of page directive insertRow method of CachedRowSet isErrorPage attribute of page directive isThreadSafe attribute of page directive JavaBean
JavaServer Pages (JSPs) javax.servlet.jsp package
javax.servlet.jsp.tagext package
JSP action
<jsp:forward> action
<jsp:getProperty> action
<jsp:include> action
<jsp:param> action
<jsp:setProperty> action
<jsp:useBean> action
jspDestroy method
jspInit method
_jspService method
JspWriter (package javax.servlet.jsp) language attribute of page directive match request parameters
meta element
moveToCurrentRow method of CachedRowSet moveToInsertRow method of CachedRowSet
name attribute of <jsp:param>
name attribute of <jsp:setProperty> name-value pair
out implicit object
page attribute of <jsp:forward> page attribute of <jsp:include> page directive
page implicit object page scope
param attribute of <jsp:setProperty> property attribute of <jsp:setProperty> refresh interval
request implicit object request scope request-time error response implicit object
scope attribute of <jsp:useBean> scope of a bean
scripting element scriptlet
setCommand method of CachedRowSet
setPassword method of CachedRowSet setUrl method of CachedRowSet setUsername method of CachedRowSet specify attributes of a custom tag standard actions
translation-time error
type attribute of <jsp:plugin> type attribute of <jsp:useBean>
updateString method of CachedRowSet value attribute of <jsp:param>
value attribute of <jsp:setProperty>
[Page 1324 (continued)]
Self-Review Exercises
27.1 Fill in the blanks in each of the following statements:
a.Action __________ has the ability to match request parameters to properties of the same name in a bean by specifying "*" for attribute property.
b.There are four key components to JSPs: __________, __________,
__________ and __________.
c.The implicit objects have four scopes: __________, __________, __________
and. __________
d.The __________ directive is processed once at JSP translation time and causes content to be copied into the JSP.
e.Classes and interfaces specific to JavaServer Pages programming are located in packages __________ and __________ .
[Page 1325]
f.JSPs normally execute as part of a Web server that is referred to as the
__________ .
g.JSP scripting components include __________, __________, __________,
__________ and __________ .
27.2 State whether each of the following is true or false. If false, explain why.
a.An object with page scope exists in every JSP of a particular Web application.
b.Directives specify global information that is not associated with a particular JSP request.
c.Action <jsp:include> is evaluated once at page translation time.
d.Like XHTML comments, JSP comments and script-language comments appear in the response to the client.
e.Objects with application scope are part of a particular Web application.
f.Each page has its own instances of the page-scope implicit objects.
g.Action <jsp:setProperty> has the ability to match request parameters to properties of the same name in a bean by specifying "*" for attribute property.
h.Objects with session scope exist for the client's entire browsing session.
[Page 1325 (continued)]
Answers to Self-Review Exercises
27.1 a) <jsp:setProperty>. b) directives, actions, scripting elements, tag libraries. c) application, page, request, session. d) include. e) javax.servlet.jsp, javax.servlet.jsp.tagext. f) JSP container. g) scriptlets, comments, expressions, declarations, escape sequences.
27.2 a) False. Objects with page scope exist only as part of the page in which they are used. b) True. c) False. Action <jsp:include> enables dynamic content to be included in a JavaServer Page. d) False. JSP comments and script-language comments are ignored and do not appear in the response. e) False. Objects with application scope are part of the JSP container application. f) True. g) True. h) True.
[Page 1325 (continued)]
Exercises
27.3Write a JSP page to output the string "Hello world!" ten times.
27.4Modify Exercise 26.6 to run as a JSP page.
27.5Rewrite Figure 27.15 to provide a form that allow users to select a book title and view the book's cover. Use a JSP expression instead of the getProperty JSP tag.
27.6Create a JSPand JDBC-based address book. Use the guest-book example of Fig. 27.20 through Fig. 27.24 as a guide. Your address book should allow the user to insert entries and search for entries. [Note: If you are not familiar with XHTML and CSS, refer to the chapters from our book Internet & World Wide Web HTP, 3/eIntroduction to XHTML: Part 1, Introduction to XHTML: Part 2 and Cascading Style Sheets (CSS), which are included as PDF documents on the CD that accompanies this book.]
27.7Reimplement the Web application of Fig. 26.21 (favorite animal survey) using JSPs. [Note: If you are not familiar with XHTML and CSS, refer to the chapters from our book Internet & World Wide Web HTP, 3/eIntroduction to XHTML: Part 1, Introduction to XHTML: Part 2 and Cascading Style Sheets (CSS), which are included as PDF documents on the CD that accompanies this book.]
27.8Modify your solution to Exercise 27.7 to allow the user to see the survey results without responding to the survey. [Note: If you are not familiar with XHTML and CSS, refer to the chapters from our book Internet & World Wide Web HTP, 3/eIntroduction to XHTML: Part 1, Introduction to XHTML: Part 2 and Cascading Style Sheets (CSS), which are included as PDF documents on the CD that accompanies this book.]
[Page 1326]
Chapter 28. Formatted Output
All the news that's fit to print.
Adolph S. Ochs
What mad pursuit?What struggle to escape?
John Keats
Remove not the landmark on the boundary of the fields.
Amenehope
OBJECTIVES
In this chapter you will learn:
To understand input and output streams.
To use printf formatting.
To print with field widths and precisions.
To use formatting flags in the printf format string.
To print with an argument index.
To output literals and escape sequences.
To format output with class Formatter.
[Page 1327]
Outline
28.1 Introduction
28.2 Streams
28.3 Formatting Output with printf
28.4 Printing Integers
28.5 Printing Floating-Point Numbers
28.6 Printing Strings and Characters
28.7 Printing Dates and Times
28.8 Other Conversion Characters
28.9 Printing with Field Widths and Precisions
28.10 Using Flags in the printf Format String
28.11 Printing with Argument Indices
28.12 Printing Literals and Escape Sequences
28.13 Formatting Output with Class Formatter
28.14 Wrap-Up
Summary
Terminology
Self-Review Exercises
Answers to Self-Review Exercises
Exercises
[Page 1327 (continued)]
28.1. Introduction
An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss the formatting features of method printf and class Formatter (package java.util). Method printf formats and outputs data to the standard output streamSystem.out. Class Formatter formats and outputs data to a specified destination, such as a string or a file output stream.
Many features of printf were discussed earlier in the text. This chapter summarizes those features and introduces others, such as displaying date and time data in various formats, reordering output based on the index of the argument and displaying numbers and strings with various flags.
