Tải bản đầy đủ (.pdf) (2 trang)

Java Server Pages 2nd Edition phần 2 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (596.04 KB, 2 trang )

Chapter 5. Generating Dynamic Content
53
Figure 5-1. JSP book examples main page

Figure 5-2. The "JSP is Easy" example output

The page shown in Example 5-1 contains both regular HTML elements and JSP elements. If
you use the View Source function in your browser, you notice that none of the JSP elements
are visible in the page source. That's because the server processes the JSP elements when the
page is requested, and only the resulting output is sent to the browser. The HTML elements,
on the other hand, are sent to the browser as-is, defining the layout of the page. To see the
unprocessed JSP page in a separate window, you can click on the source link for the easy.jsp
file in the book example's main page. The source link uses a special servlet to send the
unprocessed JSP page directly to the browser instead of letting the server process it. This
makes it easier for you to compare the source page and the processed result.
5.4 Using JSP Directive Elements
Let's look at each piece of Example 5-1 in detail. The first two lines are JSP directive
elements. Directive elements specify attributes of the page itself, such as the type of content
produced by the page, page buffering requirements, declaration of other resources used by the
page, and how possible runtime errors should be handled. Hence, a directive doesn't directly
Chapter 5. Generating Dynamic Content
54
affect the content of the response sent to the browser. There are three different JSP directives:
page, include, and taglib. In this chapter, we're using the page and the taglib
directives. The include directive is described in Chapter 16.
JSP pages typically starts with a page directive that specifies the content type for the page:
<%@ page contentType="text/html" %>
A JSP directive element starts with a directive-start identifier (<%@), followed by the directive
name (page in this case), directive attributes, and ends with %>. A directive contains one or
more attribute name/value pairs (e.g., contentType="text/html"). Note that JSP
element and attribute names are case-sensitive, and in most cases, the same is true for attribute


values. All attribute values must also be enclosed in single or double quotes.
The page directive has many possible attributes. In Example 5-1, only the contentType
attribute is used. It specifies the MIME-type for the content the page produces. The most
common values are text/html for HTML content and text/plain for preformatted,
plain text. But you can also specify other types, such as text/xml for browsers that support
XML or text/vnd.wap.wml for devices such as cell phones and PDAs that have built-in
WML browsers. The container sends the content type information to the browser as a
response header called Content-Type, so the browser knows how to interpret and render
the page. If you omit the contentType attribute, the container sets the header to
text/html.
Some of the other page directive attributes you may use from time to time are errorPage,
isErrorPage, session, pageEncoding, buffer, and autoFlush. I show you how to
use these attributes later. If you want to use scripting elements in your JSP pages, you may
also need to use the language and import attributes, covered in Chapter 15. The remaining
attributes are hardly ever used, but if you're curious, you can read about them in Appendix A.
The second directive in Example 5-1 is a taglib directive. It is used to declare a custom tag
library that is used in the page. In Example 5-1, the taglib directive declares a JSTL tag
library. The
uri attribute contains a unique string that identifies the library and the prefix
attribute defines the name prefix used for the library on this page. Let's leave it at that for the
moment; I promise to tell you more about custom tag libraries and JSTL later in this chapter.
5.4.1 JSP Comments
Example 5-1 also shows how a JSP comment looks:
<% Calculate the sum of 1 + 2 + 3 dynamically %>
Everything between <% and %> is ignored when the JSP page is processed. You can use
this type of comment to describe what's going on the page, or to temporarily comment out
pieces of the page to test different alternatives. Since a JSP comment is a JSP element, it's
never sent to the browser.


×