Download Murach's Java Servlets and JSP, 2nd Edition PDF

TitleMurach's Java Servlets and JSP, 2nd Edition
Author
LanguageEnglish
File Size15.0 MB
Total Pages752
Document Text Contents
Page 376

356 Section 2 Essential servlet and JSP skills

Other tags in the JSTL core library

Figure 1 1-10 shows six more tags in the JSTL core library. However, if you
use the MVC pattern, you probably won't need to use these tags. As a result, I've
only provided brief examples to give you an idea of how these tags work. If you
do need to use them, though, you can look them up in the documentation for the
JSTL core library as described in figure 11-2.

If you need to be able to display special characters in your JSPs, you can use
the out tag as illustrated by the first example in this figure. Then, this tag auto­
matically handles any special characters before they are displayed on the JSP. If,
for example, you try to use EL by itself to display a string that contains the left
and right angle brackets ( < > ), the JSP interprets those brackets as an liTML tag
and the string isn't displayed correctly. However, if you use the out tag, these
characters display correctly on the JSP.

If you need to set the value of an attribute in a scope, you can use the set tag.
For instance, the second example in this figure shows how to set an attribute
named message with a value of ''Test message" in session scope.

You can also use the set tag if you need to set the value of a property of an
attribute within a specified scope. However, instead of using the var attribute to
specify the name of the attribute, you use the target attribute to specify the
attribute that contains the property. To do that, you use EL within the target
attribute to specify a reference to the attribute. This is illustrated by the third
example.

The fourth example shows how to use the remove tag to remove an attribute
from a scope. When you use this tag, you use the var attribute to specify the name
of the attribute that you want to remove, and you use the scope attribute to
specify the scope that contains the attribute.

If your JSP includes code that may cause an exception to be thrown, you can
use the catch tag to catch the exceptions. This is illustrated by the fifth example.
Here, the opening and closing catch tags are coded around a Java scriptlet that
causes an ArithmeticException to be thrown due to a divide by zero error. Then,
when the exception is thrown, execution jumps over the Java expression that
displays the result of the calculation. However, the catch tag also exposes the
exception as a variable named e. As a result, the if tag that follows the catch tag is
able to display an appropriate error message.

Of course, if you edit the Java scriptlet that's in the catch tag so it performs a
legal calculation, no exception will be thrown. In that case, the result of the
calculation will be displayed and the error message won't be displayed.

The sixth example shows how to use the redirect tag to redirect a client to a
new URL. In this case, the redirect tag is coded within an if tag so the client isn't
redirected unless the condition in the if statement is true.

Although this figure doesn't include an example of the param tag, figure 1 1-3
does illustrate the use of this tag within the url tag. If you read through the
documentation for the param tag, you'll find that you can also use it with other
tags such as the import tag.

Page 377

Chapter 11 How to use the JSP Standard Tag Library (JSTL) 357

Other tags in the JSTL core library

out Uses EL to display a value, automatically handling most special characters such as the left
angle bracket ( <) and right angle bracket (> ).

set Sets the value of an attribute in a scope.
remove Removes an attribute from a scope.

catch

redirect

param

Catches any exception that occurs in its body and optionally creates an EL variable that
refers to the Throwable object for the exception.

Redirects the client browser to a new URL.
Adds a parameter to the parent tag.

An out tag that displays a message

Using the Value attribute
<c:out value="${message}" default="No message" I>

Using the tag's body
<c:out value="${message}">

No message
<IC�Out>

A set tag that sets a value in an attribute
<c: set var="message" scope="session" value="Test message" I>

A set tag that sets a value in a JavaBean

JSP code with JSTL
<c: set target="${user}" property="firstName" value="John" I>

Equivalent standard JSP tag
<jsp : setProperty name="user" property="firstName" value="John"l>

A remove tag that removes an attribute
<C� ramove var="message" scope="session" I>

A catch tag that catches an exception
<c: catch var="e">

<� II this scriptlet statement will throw an exception
int i " 110;

�>
<P>Result� <�= i �><IP>

<lc: catch>
<c:if test="${e I = null} ">

<P� exception occurred. Message� ${e.message}<IP>
<lc: if>

A redirect tag that redirects to another page
<Cdf test="${e I = null} ">

<c: redirect url="lerror_java. j sp" I>
<lc: if>

Figure 1 1 -1 o Other tags in the JSTL core library

Page 751

For more on Murach products, visit us at

murach.com

For professional developers

Murach's Java SE 6
Murach's Oracle SQL and PUSQL

Murach's Java Servlets and JSP (2nd Edition)

Murach's C# 2008

Murach's ASP.NET 3.5 Web Programming with C# 2008

Murach's Visual Basic 2008
Murach's ASP.NET 3 .5 Web Programming with VB 2008

Murach's SQL Server 2008 for Developers

Murach's OS/390 and zJOS JCL
Murach's Mainframe COBOL

Murach's CICS for the COBOL Programmer
DB2 for the COBOL Programmer, Part 1 (Second Edition)
DB2 for the COBOL Programmer, Part 2 (Second Edition)

$52.50
52.50
52.50

$52.50
52.50

52.50
52.50

52.50

$62.50

59.50
54.00
45 .00
45 .00

"Prices and availability are subject to change. Please visit our web site or call for current information.

O u r u n l i m ited g uarantee . . . when you order d i rectly from us

You must be satisfied with our books. If they aren't better than any other
programming books you've ever used . . . both for training and reference . . . you can
send them back within 90 days for a full refund. No questions asked !

Your opin ions count
If you have any comments on this book, I'm
eager to get them. Thanks for your feedback!

To com ment by

E-mai l :

We b:

Postal mai l :

m u rachbooks @ m u rach.com

www. m u rach.com

M i ke M u rach & Associates, I nc.
4340 N . Knoll

Fresno, California 93722-7825

To order now,

C Web : www. m u rach .com g


_IC:jj

IQ

Cal l toll-free :
1 -800-22 1 -5528

Fax: 1 -559-440-0963

Mike Murach & Associates, Inc.
Professional programming books

Page 752

What software you need for this book

• Java SE 6 (JDK 1.6): You can download this software for free from java.sun.com. Then,
you can install this software as described in appendix A

• Tomcat 6.0 (servlet 2.5/JSP 2.1): You can download this software for free from
tomcat.apache.org. Then, you can install and configure this software as described in
chapter 2.

• NetBeans IDE 6.0: You can download this software for free from netbeans.org. Then,
you can install and configure this software as described in chapter 3.

• MySQL 5.0: You can download this software for free from mysql.com. Then, you can
install and configure this software as described in appendix A.

• When new versions of this software become available, please check murach.com for
updates that describe how to use this book with the newer software.

The downloadable source code for this book

• Complete source code for the applications presented in this book so you can view,
compile, and run the code for the applications as you read each chapter.

• Starting source code for the exercises presented at the end of each chapter so you can get
more practice in less time.

• All source code is compatible with the N etBeans IDE.

How to download the source code for this book

• Go to murach.com, and go to the page for Murach's Java Servlets and JSP (2nd Edition).
• Click the link for "FREE download of the book applications." Then, download "All

book files." This will download a file named jsp2_allfiles.exe to your computer.

• Use the Windows Explorer to find the exe file that you downloaded. Then, double-click
this file and respond to the dialog boxes that follow. This installs the files in
subdirectories of the c:\murach\servletjsp directory.

How to use another IDE with this book

• Although we recommend using the NetBeans IDE with this book, you can use another
IDE if you prefer. To do that, you will need to figure out how to import the source code
for this book into your IDE so you can compile and run the sample applications and
complete the exercises. In addition, you will need to learn how to use your IDE to
perform the tasks presented in chapter 3.

murach.com

Similer Documents