Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading static files with servlets

 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I got a web application (ear/war file) deployed on Was 4.
I want to read a static file (.txt) from a servlet, but I don't want this static file to be directly accessible from an URL.
1)According to your experience, what is the best place to put this static file ? A sub Directory of the context-root ? or of Web-inf ?
2)I planned to put it as a sub directory of the context-root. Then, how can I "reach it" the servlet. I would like to use only relative URL.
3)Last but definitively not least question, how can I secure this folder with my static file in order to prevent a user from seing it ?
Thanks in advance for your precious contribution.
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
The only solution I found to my problem is the following :
(Load a static file from a servlet into an object property)
String propPath = getServletContext().getResource("/WEB-INF/myPropFolder/myProp.props").toString();
/* I have to cut the 6 first characterrs which are "file:/"*/
/* and I don't know why these ccharacteres are here
propPath = propPath.substring(6,propPath.length());
Properties props = new Properties();
prop.load(new FileInputStream(path));

Does this solution make sense for you ?
Any better one to suggest me ??
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WEB-INF is not browsable, unless your servlet runner is broken. So placing files in this directory will hide them.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic