File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Get member variable value from Servlet Context Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Get member variable value from Servlet Context Object" Watch "Get member variable value from Servlet Context Object" New topic
Author

Get member variable value from Servlet Context Object

Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
I have an Object in the ServletContext and now I want to get the value from that objects member variable. How do we do that?

sc.getAttribute("fileLM"));

Member varible in fileLM Object is lMod

Thanks.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Stu Higgs:
I have an Object in the ServletContext and now I want to get the value from that objects member variable. How do we do that?

sc.getAttribute("fileLM"));
Member varible in fileLM Object is lMod
Thanks.


The same way you would with any other object:


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
Thanks. I don't use get and set methods much. Maybe I should? Right now I usually just call a void method from the constructor of a class and set the member variables in the void method. My classes are really slim and specialized as a result. I do have a lot to learn about programming so I'm not even sure if the way I do this is even considered good or not.

Later when I want the value of the member variable, I use something like this which is how I implemented your code example above to achieve the result in the servlet:

//This is storing the DOM of the XML file in the servlet context.
//To keep it cached
//When the first user arrives, it will be null and therefore set
if(sc.getAttribute("myDom")==null){
sc.setAttribute("myDom", new TDOMBuilder(sXmlFilePath).document);
sc.setAttribute("fileLM",new TFileLastModified(sXmlFilePath));
}
//lets test to see if the file has been updated and if so we will rebuild //the DOM.
TFileLastModified fileLM = (TFileLastModified)sc.getAttribute("fileLM");
fLastModifiedCached = fileLM.lMod;
//I don't like calling this everytime, notify - observer - subscriber would //be nice if could be
fLastModifedTest = new TFileLastModified(sXmlFilePath).lMod;

if(fLastModifiedCached != fLastModifedTest){
sc.setAttribute("myDom", new TDOMBuilder(sXmlFilePath).document);
sc.setAttribute("fileLM",new TFileLastModified(sXmlFilePath));
}
//Transform and Print out the page
out.println(new TXMLTransformer(sXSLFilePath,sc.getAttribute("myDom")).htmlString);

Voila, it works. Thanks for all the help everyone over the past week or so. My little XML app is done!
[ January 30, 2006: Message edited by: Stu Higgs ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get member variable value from Servlet Context Object