This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use parameter defined in a web.xml?

 
Sean Li
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to use a xml file from file system, for example, a file with the name of treeview.xml, located in the WEB-INF/classes/com/yahoo/resources/, and I need to use that in a class of web layer. how cannot I get the path of that file?
later, I was wondering if I can write the path into the web.xml as a parameter. but how to use it in a class?
poor knowledge about this. pls help me. any word is apprieciable.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read a file from the WEB-INF directory of your application with something like:

where config is the ServletConfig, surveys is a directory under WEB-INF and survey.properties is the file.
Bill
 
Sean Li
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply, but how about when my class is not a servlet? in fact, i'm making a delegate to get data from ejb, then use a xml file to transform the data into something satisfy the client.
If the class is not a servlet, which means I cannot use servletConfig, so what can I do to get the path from web layer name space?
thank you again, and waiting for your reply.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the ServletContext API.
You will find the methods such as getRealPath and getResourcePaths that you can use to get the real path to a file from the web-application relative path. Get the path in your init() method and keep it around as an instance variable to be passed to your helper class.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic