Using JSP Pages with JTransit
This page explains how to take advantage of the JTransit environment
from within your JSP pages when hosting JSP pages on the same app server that's
running JTransit.
Summary
Since the JTransit servlets all use the same standard servlet session
mechanism (through the JSESSIONID cookie or searcharg) that is used by
JSP pages, it is easy to get at the JTransit-specific session information
stored for a given user. An instance of the class jtransit.core.Env is
stored in the session with a key of "userVars", so by calling
session.get( "userVars" ) in your JSP, you retrieve the JTransit session
environment for the current user.
The Env Class
The Env class is a subclass of
java.util.Hashtable,
so all methods normally available in a Hashtable (like
get,
put,
clear,
elements,
etc.) are available in Env as well.
Additional methods are available in Env for your convenience as well;
the ones most often used are getVariable and setVariable.
Object getVariable( String name )
To use getVariable, pass it the user-scope variable name
(case-insensitive), and it returns a
java.lang.Object
holding the value of the user variable, or null if no variable with
that name exists in the user scope. Call
toString()
on the non-null Object returned to retrieve the value in String form.
void setVariable( String name, Object value )
To use setVariable, pass it the user-scope variable name
(case-insensitive) and value to set it to. Nothing is returned.
You can set variables of any type; JTransit will call toString()
on objects of unknown type that it encounters, and can also
manipulate many common types of Java objects.
TagResult and TagResultArray
Many of the values returned by getVariable() will be subclasses
of jtransit.tags.TagResult, which has both toString() and getValue()
methods. All JTransit tags return TagResult objects, so
variables set using JTransit meta tags will have TagResult subclasses
as their values. The toString() method can be used to coerce
a TagResult into a String, as you would expect, but in many cases
the getValue() method is more useful (and faster) since it returns
an Object which represents the "raw" value of the TagResult.
For example, if a tag returns a TagResultLong, getValue() will return a
java.lang.Long.
Tags that return arrays will usually return a TagResultArray,
which can be manipulated by calling Object getCell(int row, int col) or
Object getCell(int row, String colName) and
void setCell(int row, int col, Object value) or void setCell(int row,
String colName, Object value). You can create a new TagResultArray() in
your own JSP or servlet code and call setNumCols(int) and setNumRows(int)
to initialize the size of the array or expand/shrink an existing array.
Both row and col are 1-based indexes, and row 0 contains column names for
each column of the array (there are no row names, and column 0 is invalid).
By accessing column or row -1, you can set or retrieve an entire row or
column of the array at once (as another TagResultArray).
|