PAUL J THOMPSON wrote:
> I started my morning by discovering something really cool...and then
> discovering that I couldn't use it. Sun's Java Tutorial on servlets
> gives great examples on how to share resources between servlets.
> However, it uses JSDK v2.1 as its base, which doesn't help much.
>
> In the interface (JSDK v2.0) there is a method called getAttribute() -
> which is GREAT for only creating one instance of an object that BY ALL
> COMMON SENSE you only need one copy of. However, the corresponding
> setAttribute() method is not included in JSDK v2.0 (rather, JSDK v2.1).
> That is no fun!
>
> So, I was wondering...in the great wisdom of this list, does ANYONE know
> how to set attributes in the current ServletContext. It seems to me that
> there should be some method by which to accomplish this or the method
> getAttribute() would not even exist for the ServletContext interface.
>
As you know, Apache JServ 1.0 only implements the 2.0 version of the
servlet API spec, and therefore does not include the
ServletContext.setAttribute() method. Work is in progress on version 1.1
(which is based on the 2.1 API), which is going to be merged with the
Sun-contributed JSWDK servlet engine code in the Jakarta project
(http://jakarta.apache.org).
>
> PLEASE HELP!!! This is fairly important. Thanks!
>
In the mean time, I would suggest that you "fake" the servlet context
attribute sharing by creating a class with static methods that uses a
Hashtable inside for storage. Because you would be using static calls, you
don't need to worry about instances. Later on, you can (and should) switch
to using ServletContext.setAttribute(), because -- among other things --
these objects are also visible to JSP pages as beans with "application"
scope.
To give you a starting point on a storage class, how about something like
this:
public final class Attributes {
private static Hashtable values = new Hashtable();
public static Object getAttribute(String name) {
return (values.get(name));
}
public static Enumeration getAttributeNames() {
return (values.keys());
}
public static void removeAttribute(String name) {
values.remove(name);
}
public static void setAttribute(String name, Object value) {
values.put(name, value);
}
}
Note that all of the method names and arguments are the same as the ones
from the ServletContext interface in 2.1, so you would do the following in
Apache JServ now:
Attributes.setAttribute("myname", myObject);
Object otherObject = Attributes.getAttribute("myname");
and, when the new API is supported later, change it to:
getServletContext().setAttribute("myname", myObject);
Object otherObject = getServletContext().getAttribute("myname");
>
> Befuddled,
> Paul J Thompson
>
Craig
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
READ THE FAQ!!!! <http://java.apache.org/faq/>
Archives and Other: <http://java.apache.org/main/mail.html/>
Problems?: [EMAIL PROTECTED]