Link to home
Start Free TrialLog in
Avatar of cassiej81
cassiej81

asked on

JSP, how to get real path of any file?

Hi,

My JSP page using a properties file call "config.properties" and i put it on WEB-INF.I used

getServletContext().getRealPath("/")+"/WEB-INF/config.properties" to get the path to it, but how can i get the path to it without declare a detail path like that.Does Jsp/servlet have any function which can scan for a file and give us a real path?

Thanks in advance!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

InputStream in = getClass().getResourceAsStream("/WEB-INF/config.properties");

and read it
Avatar of cassiej81
cassiej81

ASKER

Thanks CEHJ, but it said null :((

And what i need is a function(or anything like this) which can return a full path of this file
i.e: getFilePath("config.properties"); it's return "/root/somedir/config.propertoes"
OK what happens when you call the code you had yourself?
btw if you put your file in /WEB-INF/classes the code i posted should work
it's will return the real path of my file. I ask this question because i dont' want to change the code(to a new path) if i put the config.properties on another folder. Any solution?
That can't be done - if you change the folder, you must change the code.
Are you sure?

I still think there are have a function which can recursive all the folder and return the path of file, googling for it now
Well you could do that but it's messy. Why do you need to do this? Why not just put it in the same place each time?
Hi CEHJ,

but if i put it in WEB-INF/classes/ , there're have another way to get it without include web-inf (getClass().getResourceAsStream("/WEB-INF/config.properties");) ?
btw, i also got the null when i use your code
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, thanks CEHJ, i got it
:-)