on these JSP predefined objects. Notice that lines 13 and 14 output an HTML

tag. Although you can code HTML tags with JSP output statements, you should not do so. It’s good design practice to separate the programming logic, provided by coding JSP tags, from page formatting tags. You can read a bit more on the separation of logic from presentation later in this chapter. Listing 3-3 shows the generated HTML resulting from the JSP page in Listing 3-2. Listing 3-3: The HTML generated by the JSP page 1 2 3 4 ShowForwardAndReverse.jsp 5 6 7

You Entered Lou Marco 8

Your Name Backwards is ocraM uoL 9 10 Listing 3-4 shows another variation of the JSP page that can generate the HTML shown in Listing 3-3. Listing 3-4: A variation of the forward and backward JSP page 1 <%@ page language="java" %> 2 <%--JSP Page With a Declaration and Expressions --%> 3 4 5 ShowForwardAndReverse.jsp 6 7 8 <%-- Here is a JSP declaration --%> 9 <%! String yourName = request.getParameter("yourname") ; %> 10 <%-- Here are some JSP expressions %> 11

You Entered <%= yourName %>

<%= "Your Name Backwards is " + 12 new StringBuffer(yourName).reverse() %> 13 14 Line 2 of Listing 3-4 is a JSP comment and line 1 is the same JSP directive as coded in Listing 3-2. Again, lines 3 through 7 and 13 and 14 are straight HTML. Line 9 is an example of a JSP declaration. JSP declarations are bracketed with <%! and %>. You may code complete Java declare statements and complete Java method code in a JSP declaration. Line 9 shows the declaration and initialization of the string variable containing the string entered in the HTML form. Lines 11 and 12 are examples of JSP expressions. A JSP expression is compiled to an output statement that writes a string. The string is formed by evaluating the expression sandwiched between <@= and %>. JSP expressions use the toString() method to convert non-string objects to strings for output, as in the following expression: <%= yourName %> produces the same output as the scriptlet below: <% out.println( yourName ) ; %>
Our window web hosting plans are bursting with features and FREE tools, at the very small rate. Sign up today window web hosting.

Leave a Reply