File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet Context setAttribute getAttribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Context setAttribute getAttribute" Watch "Servlet Context setAttribute getAttribute" New topic

Servlet Context setAttribute getAttribute

Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
I'm trying to return a DOM and store it in the ServletContext via

sc.setAttribute("myDom", new TDOMBuilder(sXmlFilePath));

no problem so far, but I am not sure whether this is even allowed or not?

Then, I am trying to send that DOM stored in myDom to a method that will use the DOM in an XSLT transform like this:

new TXMLTransformer(sXSLFilePath, sc.getAttribute("myDom"))

My question is how do I get the constructor in TXMLTransformer to see sc.getAttribute("myDom") as a Document type without a ClassCastException?

I have tried to set the constructor arg to Document, but then compiler complains that TXMLTransformer is looking for an Object type arg not supplied. If changing to Object in the TXMLTransformer arg, then later in TXMLTransformer how do I get that object to be of Document Type without ClassCastException?



I believe I have solved the problem by using this:

sc.setAttribute("myDom", new TDOMBuilder(sXmlFilePath).document);

prior to that I think I was just returning the Object and not the reference to the member variable in the object.

Now I am wondering if I will be able to access myDom via sc.getAttribute("myDom") from any servlet, and is it now stored similarto a session object?

[ January 30, 2006: Message edited by: Stu Higgs ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

The get and setAttribute methods of ServletContext work the same way they do for the session or request scopes (except for their scope, of course).
You're just binding an object to a map and then retrieving it with a key.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
I agree. Here's the link:
subject: Servlet Context setAttribute getAttribute
It's not a secret anymore!