JSP.1.2 What Is a JSP Page?


A JSP page is a text-based document that describes how to process a request to create a response . The description intermixes template data with some dynamic actions and leverages on the Java platform.

The features in the JSP technology support a number of different paradigms for authoring of dynamic content; some of them are described in Section JSP.1.6. The next couple of examples only attempt to present the technical components of the JSP specification and are not prescribing "good" or "bad" paradigms.

JSP.1.2.1 An Example Using Scripting and Beans

A simple example of a JSP page is shown in Figure JSP.1-1. The example shows the response page, which is intended to be a short list with the day of the month and year at the moment when the request is received. The page itself contains fixed template text and additional elements described by the JSP specification that are shown underlined in the figure. As the request reaches the page, the response is created based on the template text. As the first element is reached, a server-side bean object is created with name clock and type calendar.jspCalendar. This object can be used and modified later in the page. In particular, the next two elements access properties of the object and insert these values into the response page as strings.

Figure JSP.1-1. A JSP Page Using Beans and Scripting

JSP.1.2.2 An Example Using a Tag Library

Figure JSP.1-2 is another example of a JSP page. This page uses custom actions to create the server-side object and then to produce the response data. In the example, a taglib directive first makes available into this page a tag library for database queries. The directive indicates the tag library to use and provides a prefix to use locally in this page to name those actions.

Figure JSP.1-2 A JSP Page Using Custom Actions
 <html>  <%@ taglib uri="http://acme.com/taglibs/simpleDB.tld" prefix="x" %>  <x:queryBlock connData="conData1">      <x:queryStatement>          SELECT ACCOUNT, BALANCE FROM ...      </x:queryStatement>      The top 10 accounts and balances are:      <table>          <tr><th>ACCOUNT</th><th>BALANCE</th></tr>          <x:queryCreateRows from="1" to="10">              <td><x:queryDisplay field="ACCOUNT"/></td>              <td><x:queryDisplay field="BALANCE"/></td>          </x:queryCreateRows>      </table>  </x:queryBlock>  </html> 

Designing tag libraries is a delicate effort, analogous to that of designing a language; we are making no special effort here to define tags that are useful for any but pedagogical purposes. For the purposes of this example, we will assume that this fictitious tag library introduces four actions:

A queryBlock action introduces a database connection; it can contain query Statement actions and queryCreateRow actions. The connData attribute refers to connection-specific data, like login and password, that are to be defined elsewhere.

A queryStatement action must be enclosed in a queryBlock . A queryStatement's body is a SQL statement; it will use the connection data defined in the enclosing queryBlock .

A queryCreateRows action must be enclosed in a queryBlock action. A queryCreateRows action will iterate over the results of the last executed query and will generate up to as many rows as requested .

A queryDisplay action must be enclosed in a queryCreateRows action. A queryDisplay action will access the requested field from the current iteration in queryCreateRows and insert the value into the out object.

In this example:

  • The x:queryCreateRows action implicitly refers to the object created by the x:queryStatement within the same x:queryBlock .

  • The x:queryDisplay actions refer to the current row in the query result that is being iterated over by x:queryCreateRows .

  • The code that locates a connection (perhaps from a connection pool), performs the JDBC API query, and navigates through the result of this query is hidden in the implementation of the custom actions. This encourages division of labor and isolation from changes.

JSP.1.2.3 Components and Containers

The JavaServer Pages technology builds on the servlet standard extension. JavaServer Pages is a standard extension that is defined extending the concepts in the servlet standard extension. JSP 1.1 uses the classes from Java Servlet 2.2 specification.

JSP pages and servlet classes are collectively referred to as web components . JSP pages are delivered to a container that provides the services indicated in the JSP component contract .

JSP 1.1 and Servlet 2.2 rely only on features in the Java Runtime Environment 1.1, although they are compatible with, and can take advantage of, the Java 2 Runtime Environment.



Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net