Explaining Attributes |
![]() |
Attributes in GNU-Paperclips have magic qualities that normal servlet
engine attributes do not have. This section describes those differences
in (hopefully) as concise and helpfull a manner as possible.
| Class Attribute
{ String name; Object value; } |
This exactly mirrors the servlet-context definition of what an attribute is. You can't talk to an attribute's properties directly because they are private, instead there are methods:
| Class Attribute
{ //set and get the associated value void setValue(Object value); Object get(); //return the value as a String
//get the name
|
The .toString() method needs some explanation. .toString() can be assumed to always return the correct String representation of the value object because it calls the value object's own .toString() method.
There are several Attribute types (one plain, one for integers and one for booleans) and they usually declare an approproate constructor, the IntAttribute looks like this:
|
IntAttribute(String name,int value); |
All of this means that you can easily declare Attribute objects in your code.
None of that would make any sense though if you could not access those Attributes in the usual way, ie: through the ServletContext get/set methods.
| void setAttribute(String attrname,Object value) | the standard ServletContext method for setting an attribute, a context specific registry is updated or added to with the value; the registry contains only objects of class Attribute (or sub-classes of it) and this method is responsible for creating an Attribute wrapper for the specified Object; |
| Attribute setAttribute(Attribute a) | adds the specifed Attribute object to the registry; the name and value are both encapsulated within the Attribute object. |
| IntAttribute setAttribute(IntAttribute a) | same as the vanilla Attribute method but specifically for integer based attributes. |
| BooleanAttribute setAttribute(BooleanAttribute a) | same as the vanilla Attribute method but specifically for boolean based attributes. |
These methods allow you to enter attributes you create into the ServletContext registry.
The ServletContext.getAttribute(String name) method is designed
to return the value of the attribute which is located in the registry by
name.
It will find the Attribute object in the registry (by searching
all the Attribute objects in the registry and matching
name)
and will then return the value of the Attribute with the Attribute
method .get().
It works very simply, when you create an Attribute you can specify a
type of access, the access levels are defined in class Attribute and are:
| UNPROTECTED | the attribute's value can be altered and (if it is in the registry) then the attribute can be removed from the registry; |
| NODELETION | if the attribute is in the registry then it cannot be removed, it's value can still be changed however; |
| NOALTERATION | the value of the attribute cannot be changed even by setAttribute(name,value) neither can it be removed from the registry, once in your stuck with it. |
Since this is only available to extenders right now I won't go on about
it too much. However, it is there and if I get enough interest this feature
will get promoted to public accessability.
The current method seemed the simplest choice for the functionality.
Though because this is a bit dirty I don't expect it to be added to the
Servlet API in the near future. Perhaps you can think of an improvement
to the system?
Of course this will only be able to take a number of different base types as values (strings, integers, booleans).
| Created: 1 July 1999 | Last Updated: | Contact: webmaster | Author: Nic Ferrier |