JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle GlassFish Server 3.1 Application Development Guide
search filter icon
search icon

Document Information

Preface

Part I Development Tasks and Tools

1.  Setting Up a Development Environment

2.  Class Loaders

3.  Debugging Applications

Part II Developing Applications and Application Components

4.  Securing Applications

5.  Developing Web Services

6.  Using the Java Persistence API

7.  Developing Web Applications

Using Servlets

Caching Servlet Results

Caching Features

Default Cache Configuration

Caching Example

The CacheKeyGenerator Interface

About the Servlet Engine

Instantiating and Removing Servlets

Request Handling

Using JavaServer Pages

JSP Tag Libraries and Standard Portable Tags

JSP Caching

Enabling JSP Caching

Caching Scope

The cache Tag

The flush Tag

Options for Compiling JSP Files

Creating and Managing Sessions

Configuring Sessions

HTTP Sessions, Cookies, and URL Rewriting

Coordinating Session Access

Saving Sessions During Redeployment

Logging Session Attributes

Distributed Sessions and Persistence

Session Managers

The memory Persistence Type

The file Persistence Type

The replicated Persistence Type

The coherence-web Persistence Type

Using Comet

Introduction to Comet

The Grizzly Implementation of Comet

Client Technologies to Use With Comet

Types of Comet Connections

Grizzly Comet

The Grizzly Comet API

The Hidden Frame Example

Creating a Comet-Enabled Application

Developing the Web Component

Creating the Client Pages

Creating the Deployment Descriptor

Deploying and Running a Comet-Enabled Application

Bayeux Protocol

Enabling Comet

To Configure the web.xml File

To Write, Deploy, and Run the Client

Advanced Web Application Features

Internationalization Issues

The Server's Default Locale

Servlet Character Encoding

Virtual Server Properties

Class Loader Delegation

Using the default-web.xml File

To Use the default-web.xml File

Configuring Logging and Monitoring in the Web Container

Configuring Idempotent URL Requests

Specifying an Idempotent URL

Characteristics of an Idempotent URL

Header Management

Configuring Valves and Catalina Listeners

Alternate Document Roots

Using a context.xml File

Enabling WebDav

Using SSI

Using CGI

8.  Using Enterprise JavaBeans Technology

9.  Using Container-Managed Persistence

10.  Developing Java Clients

11.  Developing Connectors

12.  Developing Lifecycle Listeners

13.  Developing OSGi-enabled Java EE Applications

Part III Using Services and APIs

14.  Using the JDBC API for Database Access

15.  Using the Transaction Service

16.  Using the Java Naming and Directory Interface

17.  Using the Java Message Service

18.  Using the JavaMail API

Index

Using JavaServer Pages

The GlassFish Server supports the following JSP features:

For information about creating JSP files, see the JavaServer Pages web site at http://www.oracle.com/technetwork/java/javaee/jsp/index.html.

For information about Java Beans, see the JavaBeans web page at http://www.oracle.com/technetwork/java/javase/tech/index-jsp-138795.html.

This section describes how to use JavaServer Pages (JSP files) as page templates in a GlassFish Server web application.

The following topics are addressed here:

JSP Tag Libraries and Standard Portable Tags

GlassFish Server supports tag libraries and standard portable tags. For more information, see the JavaServer Pages Standard Tag Library (JSTL) page at http://www.oracle.com/technetwork/java/index-jsp-135995.html.

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

However, the as-install/lib/jspcachtags.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.

The following topics are addressed here:

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

Enabling JSP Caching

To globally enable JSP caching, set the jspCachingEnabled property to true. The default is false. For example:

asadmin set server-config.web-container.property.jspCachingEnabled="true"

For more information about the asadmin set command, see the Oracle GlassFish Server 3.1-3.1.1 Reference Manual.

To enable JSP caching for a single web application, follow these steps:

  1. Extract the META-INF/jspcachtags.tld file from the as-install/glassfish/modules/web-glue.jar file.

  2. Create a new JAR file (for example, jspcachtags.jar) containing just the META-INF/jspcachtags.tld file previously extracted.

  3. Bundle this new JAR file in the WEB-INF/lib directory of your web application.


Note - Web applications that use JSP caching without bundling the tag library are not portable.


Refer to GlassFish Server tags in JSP files as follows:

<%@ taglib prefix="prefix" uri="http://glassfish.org/taglibs/cache" %>

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>.

Caching Scope

JSP caching is available in three different scopes: request, session, and application. The default is application. To use a cache in request scope, a web application must specify the com.sun.appserv.web.taglibs.cache.CacheRequestListener in its web.xml deployment descriptor, as follows:

<listener>
   <listener-class>
      com.sun.appserv.web.taglibs.cache.CacheRequestListener
   </listener-class>
</listener>

Likewise, for a web application to utilize a cache in session scope, it must specify the com.sun.appserv.web.taglibs.cache.CacheSessionListener in its web.xml deployment descriptor, as follows:

<listener>
   <listener-class>
      com.sun.appserv.web.taglibs.cache.CacheSessionListener
   </listener-class>
</listener>

To utilize a cache in application scope, a web application need not specify any listener. The com.sun.appserv.web.taglibs.cache.CacheContextListener is already specified in the jspcachtags.tld file.

The cache Tag

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 of cache

The following table describes attributes for the cache tag.

Table 7-1 The 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.
scope
application
(optional) The scope of the cache. Can be request, session, or application. See Caching Scope.
Example of cache

The following example represents a cached JSP file:

<%@ taglib prefix="mypfx" uri="http://glassfish.org/taglibs/cache" %>
<%@ 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>

The flush Tag

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 of flush

The following table describes attributes for the flush tag.

Table 7-2 The 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.
scope
application
(optional) The scope of the cache. Can be request, session, or application. See Caching Scope.
Examples of flush

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

GlassFish Server provides the following ways of compiling JSP source files into servlets: