ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

JavaServer Pages: Generating Dynamic Content
Pages: 1, 2, 3, 4, 5, 6, 7, 8

Using JSP Directives

The first line in Example 5-1 is a JSP directive element. Directives are used to specify attributes of the page itself, primarily those that affect how the page is converted into a Java servlet. There are three JSP directives: page, include, and taglib. In this example, we're using only the page directive. We'll see the others later.

JSP pages typically start with a page directive that specifies the scripting language and the content type for the page:

<%@ page language="java" contentType="text/html" %>

A JSP directive element starts with a directive-start identifier (<%@) followed by the directive name (e.g., page) and directive attributes, and ends with %>. A directive contains one or more attribute name/value pairs (e.g., language="java"). Note that JSP element and attribute names are case-sensitive, and in most cases the same is true for attribute values. For instance, the language attribute value must be java, not Java. All attribute values must also be enclosed in single or double quotes.

The page directive has many possible attributes. In Example 5-1, two of them are used: language and contentType.

The language attribute specifies the scripting language used in the page. The JSP reference implementation (the Tomcat server) supports only Java as a scripting language.[1] java is also the default value for the language attribute, but for clarity you may still want to specify it. Other JSP implementations support other languages besides Java, and hence allow other values for the language attribute. For instance, both JRun (http://www.allaire.com/) and Resin (http://www.caucho.com/) support JavaScript in addition to Java.

The contentType attribute specifies the type of 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 like cellular phones and PDAs that have built-in Wireless Markup Language (WML) browsers. If the content generated by the page includes characters requiring a charset other than ISO-8859-1 (commonly known as Latin-1), you need to specify that charset with the contentType attribute. We'll look at the details of charsets in Chapter 11, Internationalization.

Using Template Text

Besides JSP elements, notice that the page shown in Example 5-1 contains mostly regular HTML:

  <body bgcolor="white">
    The current time at the server is:
      <li>Date: ...
      <li>Month: ...
      <li>Year: ...
      <li>Hours: ...
      <li>Minutes: ...

In JSP parlance, this is called template text. Everything that's not a JSP element, such as a directive, action, or scripting element, is template text. Template text is sent to the browser as-is. This means you can use JSP to generate any type of text-based output, such as XML, WML, or even plain text. The JSP container doesn't care what the template text is.

Pages: 1, 2, 3, 4, 5, 6, 7, 8

Next Pagearrow