Part I Development Tasks and Tools
1. Setting Up a Development Environment
Part II Developing Applications and Application Components
6. Using the Java Persistence API
7. Developing Web Applications
The CacheKeyGenerator Interface
Instantiating and Removing Servlets
Creating and Managing Sessions
HTTP Sessions, Cookies, and URL Rewriting
Saving Sessions During Redeployment
Distributed Sessions and Persistence
The replicated Persistence Type
The coherence-web Persistence Type
The Grizzly Implementation of Comet
Client Technologies to Use With Comet
Creating a Comet-Enabled Application
Creating the Deployment Descriptor
Deploying and Running a Comet-Enabled Application
To Write, Deploy, and Run the Client
Advanced Web Application Features
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
Characteristics of an Idempotent URL
Configuring Valves and Catalina Listeners
8. Using Enterprise JavaBeans Technology
9. Using Container-Managed Persistence
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
The GlassFish Server supports the following JSP features:
JavaServer Pages (JSP) Specification
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 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:
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 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.
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:
Extract the META-INF/jspcachtags.tld file from the as-install/glassfish/modules/web-glue.jar file.
Create a new JAR file (for example, jspcachtags.jar) containing just the META-INF/jspcachtags.tld file previously extracted.
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>.
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 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.
The following table describes attributes for the cache tag.
Table 7-1 The cache Attributes
|
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>
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.
The following table describes attributes for the flush tag.
Table 7-2 The flush Attributes
|
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>
GlassFish Server provides the following ways of compiling JSP source files into servlets:
JSP files are automatically compiled at runtime.
The asadmin deploy command has a ----precompilejsp option. For details, see the Oracle GlassFish Server 3.1-3.1.1 Reference Manual.
The jspc command line tool allows you to precompile JSP files at the command line. For details, see the Oracle GlassFish Server 3.1-3.1.1 Reference Manual.