using ServletContext.setAttribute(name, object)
843840Feb 7 2002 — edited Feb 7 2002Hi All,
I am running tomcat 4 on Win2000.
I need to create an instance of a 3rd Party Java Object, an extraction "Engine". I can only create one instance of this, as the classloader will not permit it to be declared mroe than once.
In order for different servlet requests to use this engine without creating an instance, I store it in the servletContext using:
ServletContext.setAttribute("Engine", EngineObject);
I can then retrieve it in any servlet by using:
Object obj = ServletContext.getAttribute("Engine");
The problem is that I 'occasionally' get a classCastException trying to cast it back into the 'Engine' object type it really is. i.e.
EngineObject e =(EngineObject)obj;
So basically I think the object sometimes get corrupted within tomcat.
Has anyone succesully used the servetContext to store persistant objects within tomcat, without the objects getting corrupted?
P.S. I cannot store it in a session, as multiple sessions need access to it.
Thanks in advance
James Manna
Power Solutions DTD Pty Ltd
JamesManna@power-solutions.com.au