Sun Java System Application Server Platform Edition 8.1 2005Q2 Update 2 Developer's Guide

- Previous: Using Servlets
- Next: Creating and Managing HTTP Sessions

# Using JavaServer Pages

The Application Server supports the following JSP features:

- JavaServer Pages (JSP) Specification version 2.0

- Precompilation of JSP files, which is especially useful for production servers

- JSP tag libraries and standard portable tags

For information about creating JSP files, see Sun Microsystem’s JavaServer Pages web site at `http://java.sun.com/products/jsp/index.html`.

For information about Java Beans, see Sun Microsystem’s JavaBeans web page at `http://java.sun.com/beans/index.html`.

This section describes how to use JavaServer Pages (JSP files) as page templates in an Application Server web application. This section contains the following topics:

- JSP Tag Libraries and Standard Portable Tags

- JSP Caching

- Options for Compiling JSP Files

## JSP Tag Libraries and Standard Portable Tags

Application Server supports tag libraries and standard portable tags. For more information, see the JavaServer Pages Standard Tag Library (JSTL) page at `http://java.sun.com/products/jsp/jstl/index.jsp`.

Web applications don’t need to bundle copies of the `jsf-impl.jar` or `appserv-jstl.jar` JSP tag libraries (in install-dir`/lib`) to use JavaServerTM Faces technology or JSTL, respectively. These tag libraries are automatically available to all web applications.

However, the install-dir`/lib/``appserv-tags.jar` tag library for JSP caching is not automatically available to web applications. See JSP Caching, next.

## JSP Caching

JSP caching lets you cache tag invocation results within the Java engine. Each can be cached using different cache criteria. For example, suppose you have invocations to view stock quotes, weather information, and so on. The stock quote result can be cached for 10 minutes, the weather report result for 30 minutes, and so on.

For more information about response caching as it pertains to servlets, see Caching Servlet Results.

JSP caching is implemented by a tag library packaged into the install-dir`/lib/``appserv-tags.jar` file, which you can copy into the `WEB-INF/lib` directory of your web application. The `appserv-tags.tld` tag library descriptor file is in the `META-INF` directory of this JAR file.

Note –

Web applications that use this tag library are not portable.

To allow all web applications to share this tag library, change the following elements in the `domain.xml` file. Change this:

    <jvm-options>
    -Dcom.sun.enterprise.taglibs=appserv-jstl.jar,jsf-impl.jar
    </jvm-options>

to this:

    <jvm-options>
    -Dcom.sun.enterprise.taglibs=appserv-jstl.jar,jsf-impl.jar,appserv-tags.jar
    </jvm-options>

and this:

    <jvm-options>
    -Dcom.sun.enterprise.taglisteners=jsf-impl.jar
    </jvm-options>

to this:

    <jvm-options>
    -Dcom.sun.enterprise.taglisteners=jsf-impl.jar,appserv-tags.jar
    </jvm-options>

For more information about the `domain.xml` file, see the Sun Java System Application Server Platform Edition 8.1 2005Q2 Update 2 Administration Reference.

Refer to these tags in JSP files as follows:

    <%@ taglib prefix="prefix" uri="Sun ONE Application Server Tags" %>

Subsequently, the cache tags are available as `<`prefix`:cache>` and `<`prefix`:flush>`. For example, if your prefix is `mypfx`, the cache tags are available as `<mypfx:cache>` and `<mypfx:flush>`.

The tags are as follows:

- cache

- flush

### cache

The cache tag caches the body between the beginning and ending tags according to the attributes specified. The first time the tag is encountered, the body content is executed and cached. Each subsequent time it is run, the cached content is checked to see if it needs to be refreshed and if so, it is executed again, and the cached data is refreshed. Otherwise, the cached data is served.

#### Attributes

The following table describes attributes for the `cache` tag.

Table 5–2 `cache` Attributes

Attribute | Default | Description
--- | --- | ---
`key` | ServletPath`_`Suffix | (optional) The name used by the container to access the cached entry. The cache key is suffixed to the servlet path to generate a key to access the cached entry. If no key is specified, a number is generated according to the position of the tag in the page.
`timeout` | `60s` | (optional) The time in seconds after which the body of the tag is executed and the cache is refreshed. By default, this value is interpreted in seconds. To specify a different unit of time, add a suffix to the timeout value as follows: `s` for seconds, `m` for minutes, `h` for hours, `d` for days. For example, `2h` specifies two hours.
`nocache` | `false` | (optional) If set to `true`, the body content is executed and served as if there were no `cache` tag. This offers a way to programmatically decide whether the cached response is sent or whether the body has to be executed, though the response is not cached.
`refresh` | `false` | (optional) If set to `true`, the body content is executed and the response is cached again. This lets you programmatically refresh the cache immediately regardless of the `timeout` setting.

#### Example

The following example represents a cached JSP file:

    <%@ taglib prefix="mypfx" uri="Sun ONE Application Server Tags" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <mypfx:cache                 key="${sessionScope.loginId}"
    			nocache="${param.nocache}"
    			refresh="${param.refresh}"
    			timeout="10m">
    <c:choose>
    	<c:when test="${param.page == 'frontPage'}">
    		<%-- get headlines from database --%>
    	</c:when>
    	<c:otherwise>
    		...
    	</c:otherwise>
    </c:choose>
    </mypfx:cache>
    <mypfx:cache timeout="1h">
    <h2> Local News </h2>
    	<%-- get the headline news and cache them --%>
    </mypfx:cache>

### flush

Forces the cache to be flushed. If a `key` is specified, only the entry with that key is flushed. If no key is specified, the entire cache is flushed.

#### Attributes

The following table describes attributes for the `flush` tag.

Table 5–3 `flush` Attributes

Attribute | Default | Description
--- | --- | ---
`key` | ServletPath`_`Suffix | (optional) The name used by the container to access the cached entry. The cache key is suffixed to the servlet path to generate a key to access the cached entry. If no key is specified, a number is generated according to the position of the tag in the page.

#### Examples

To flush the entry with `key="foobar"`:

    <mypfx:flush key="foobar"/>

To flush the entire cache:

    <c:if test="${empty sessionScope.clearCache}">
       <mypfx:flush />
    </c:if>

#### Options for Compiling JSP Files

Application Server provides the following ways of compiling JSP 2.0 compliant source files into servlets:

- JSP files are automatically compiled at runtime.

- The `asadmin deploy` command has a `precompilejsp` option. For details, see the Sun Java System Application Server Platform Edition 8.1 2005Q2 Update 2 Reference Manual.

- The `sun-appserv-jspc` Ant task allows you to precompile JSP files; see sun-appserv-jspc.

- The `jspc` command line tool allows you to precompile JSP files at the command line. For details, see the Sun Java System Application Server Platform Edition 8.1 2005Q2 Update 2 Reference Manual.

- Previous: Using Servlets
- Next: Creating and Managing HTTP Sessions

- © 2010, Oracle Corporation and/or its affiliates