Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Filter: how to resolve path

 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi-
I'm writing a couple of filters that our apps will use.
I want each app to provide for its own config.fil under /web-inf/.
My filters will be deployed as a jar on WAS.
In my doFilter method I want to resolve the path to the comfig file
e.g. (in doFilter)
String cfgFilepath =
httpReq.getServerName()
+httpReq.getServerPort()
+httpReq.getContextPath()
+"/WEB-INF/redirect-config.xml";
Parser p = new Parser(); //a SAX parser
p.processFile(cfgFilepath);
p.listData();

I'm not getting close to my file.
Anyone know a way to do this?
many thanks,
max
 
Steve Leach
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you use FilterConfig.getServletContext().getRealPath(fileName) ?
 
Steve Leach
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More specifically...

where configFile is a String field of the class.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you care what the "real" path is? Why not use ServletContext.getResource() which returns a URL object representing a resource relative to the document root (which never changes for your webapp). That should get the job done.
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That'll do nicely, sir.
Thanks for this very helpful tip.
Max Tomlinson
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic