caucho
 JSP Applications

  1. Introduction
  2. .application directory
    1. Scripts
    2. Data
    3. Tags
  3. global.jsa
    1. Event Handlers
  4. Sessions
  5. JSP Beans
    1. Accessing Beans

Introduction

JSP groups pages into applications which share common state. A bulletin board application groups pages for article reading, group listing, user registration, and new article posting into a single application.

Applications can keep track of user sessions, giving the users the illusion of a single application out of disjointed pages.

  • JSP organizes applications with an .application directory.
  • The global.jsa file contains common declarations and procedures for starting and stopping sessions and applications.
  • Sessions share state for a user's visit to the site.

.application directory

The .application directory organizes an application. It defines an application docroot by its presense. All files and directories in the docroot are part of the same application.

.application contents
File/Directory Contents
global.jsa control file
scripts common scripts
data file data, e.g. text and xml
beans Java class files for beans
tags Java class files for tag compilation
Example directory structure
newsgroup/              // application docroot
  .application/         // application data
    global.jsa          // global control
    scripts/            // included scripts
      common.es
    data/               // application storage
      art1.xml          // example article
      art2.xml          // another article
    beans/              // application beans
      article.class     // Java article code
    tags/               // application tags
      dateHandler.class // date tag
  list.jsp              // list articles
  read.jsp              // read an article
  post.jsp              // post an article

Scripts

JSP applications can group common functions and classes into script files, organizing and simplifying the application. The individual pages can then import the common scripts to use them. JSP pages can focus on HTML presentation and the script libraries can concentrate on the content.

As described in the import documentation, Resin searches for imported scripts along a SCRIPTPATH. For JSP pages, the SCRIPTPATH looks in the application script directory first, then in the global libraries.

JSP SCRIPTPATH
  $APPDIR/.application/scripts
    :$RESIN_HOME/scripts

Data

The data subdirectory of .application can contain any data the JSP application needs to store. It could contain persistent state for a counter, to the entire article tree for a complicated discussion server.

The File object in a JSP script uses .application/data as its current directory.

Tags

JSP 1.0 provides an extensible tag architecture, allowing applications to design their own active tags. The tag handlers are Java classes extending the ChunkHandler interface. When the JSP parser encounters a new tag, it looks in tags for a matching class.

global.jsa

The global.jsa file contains common declarations, application and session bean declarations, and event handlers. Its structure resembles to a JSP file, except that it never creates content.

Session and application beans must be declared in a global.jsa jsp:usebean directive. Only page beans may be declared in a *.jsp file. The application beans will be instantiated when the first page of the application is accessed and will persist until the server shuts down.

Error pages global to the entire application may be declared in the global.jsa file. Local error page declarations override the global declarations.

The language declared in the global.jsa file is local to the global file. It only declares the language for the declaration section and the event handlers.

Event Handlers

Applications can declare event handlers to execute when an application starts or stops and when a session starts and stops. The event handlers can initialize state, read data from storage, and store the final state when the server shuts down.

Applications start on the first page access. The application event handlers jsp:application:onStart and jsp:application:onEnd can use the application object.

Sessions start when a page accesses session state or when a session bean is created. Sessions end when the server times them out, when the application ends, or when they are invalidated. The session event handlers jsp:session:onStart and jsp:session:onEnd can use the session and application objects.

Persistent Storage of Counter
<jsp:application:onStart>
  var file = File("counter");
  if (file.exists()) {
    var is = file.openRead();
    var counter = new Number(is.readln());
    is.close();
  } else
    var counter = 0;

  application.attributes.counter = counter;
</jsp:application:onStart>

<jsp:application:onEnd>
  var os = File("counter").openWrite();
  os.writeln(application.attributes.counter);
  os.close();
</jsp:application:onEnd>

Sessions

Session variables let applications keep track of the user as she moves through the site. Any e-commerce site needs this capability to keep track of the user's purchases.

JSP sessions start when the page accesses the session variable. If a page never uses sessions, the jsp:session:onStart code will not execute and the web server will not send the client any cookies.

Sessions end when the session times out, when the session is invalidated, or when the application ends.

Resin locks the session variable before executing the page. So JSP applications don't need to worry about synchronizing the session variable.

JSP Beans

Java Beans get first class treatment in JSP 1.0. Beans can be created for a page, across a session, or for the entire application.

The .application/beans subdirectory can contain application beans used by jsp:usebean. These are simply Java classes implementing the bitmechanic work of an application.

For example, a shopping cart application may have a set of Java classes that perform the security necessary for credit card processing. The application can put those classes in the beans directory and access them from the JSP page. Beans can be created with different lifetimes.

  • Application beans last the lifetime of an application.
  • Session beans last for a user's session.
  • Page beans only last for a single page.

Accessing Beans

Each bean is defined with a jsp:usebean directive. Page beans are defined in the JSP page. Session and application beans are defined in the global.jsa file.

JSP assigns the created bean object to the JavaScript variable named by jsp:usebean.

In addition, the created beans are stored in JSP variables: page beans are stored in request, session beans are stored in session, and application beans are stored in application. Storing the beans in the JSP variables lets other beans and functions retrieve the beans.

Beans in variables: test.jsp
<jsp:usebean name='test' class='java.util.Hashtable'>

<% test.put("a", 1); %>
<%= test.get("a"); %>

<jsp:usebean name='test' class='java.util.Hashtable'>

<% 
  var t = request.attributes["beans/test"] 
  t.put("a", 1);
%>
<%= test.get("a"); %>
1

 
Copyright © 1998-2000 Caucho Technology. All rights reserved.
Last modified: Thu, 16 Sep 1999 14:56:48 -0700 (PDT)