Java Reference
In-Depth Information
Using a String with a JSP String Tag
As shown in Figure 12-70 on page 846, much of the JavaServer Page is
HTML, just like the HTML page coded earlier. Sections of the page where Java
code is embedded are enclosed within JSP tags.
The JSP string tag , <%=, must be followed by an expression that evaluates
to a string. Because a session attribute is stored as an Object, it must be down-
cast to the appropriate object type before its value can be used in code. In the
code for mainForm.jsp, the code in line 47 in Figure 12-65 on page 841 down-
casts the session attribute value, uses, to an Integer object because the session
attribute originally was assigned an Integer object. It then retrieves the int value,
not a String, which is used within the JSP string tag inserted in the message dis-
played by the alert() method in lines 49 through 51. The expression, uses, is an
int; however, when an integer is output, the String.valueOf(int) method is called
implicitly, obtaining a String value just as with System.out. This leaves the int
variable, uses, available for numeric manipulation, if necessary. If a String vari-
able were used, its value could not be changed because a String is immutable.
Line 80 in Figure 12-70 also uses a String — retrieving the session attribute,
userName, casting it to a String, and placing it within a JSP string tag for direct
output on the Web page.
The following step enters the code to use a String with a JSP string tag.
To Use a String with a JSP String Tag
1. Enter lines 61 through 151 as shown in Figure 12-70.
TextPad displays scriptlet and string tags used in the JSP (Figure 12-72).
Code within scriptlet tags is just like code in a servlet. Code within a JSP
string tag must evaluate to a String or the JSP will not compile.
JSP scriptlet
tag
JSP scriptlet
tag
JSP scriptlet
tag
JSP string tag
JSP scriptlet
tag
String downcast
FIGURE 12-72
 
Search WWH ::




Custom Search