Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get member variable value from Servlet Context Object

 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic