• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get member variable value from Servlet Context Object

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic