JSP.5.6 Cooperating Actions


Often two actions in a JSP page want to cooperate, perhaps by one action creating some server-side object that is used by the other. There are two basic mechanisms in the JSP specification to achieve this.

JSP.5.6.1 Ids and PageContext

One mechanism for supporting cooperation among actions is by giving the object a name within the JSP page; the first action creates and names the object while the second action uses the name to retrieve the object.

For example, in the following JSP page fragment the foo action creates a server-side object and give it the name " myObject ." Then the bar action accesses that server-side object and takes some action.

 <x:foo id="myObject" />  <x:bar ref="myObject" /> 

In a JSP container implementation, the mapping between the name and the value is kept by the implicit object pageContext . This object is passed around through the tag handler instances so it can be used to communicate information: All that is needed is to know the name under which the information is stored into the pageContext .

JSP.5.6.2 Run-Time Stack

An alternative to explicit communication of information through a named object is implicit coordination based on syntactic scoping.

For example, in the following JSP page fragment the foo action might create a server-side object; later the nested bar action might access that server-side object. The object is not named within the pageContext : it is found because the specific foo element is the closest enclosing instance of a known element type.

 <foo>    <bar/>  </foo> 

This functionality is supported through the findAncestorWithClass(Tag, Class) static method of the Tag class, which uses a reference to parent tag kept by each Tag instance, which effectively provides a runtime execution stack.



Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net