We help IT Professionals succeed at work.

JSP, how to get real path of any file?

cassiej81
cassiej81 asked
on
13,990 Views
Last Modified: 2013-11-24
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!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Try

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

and read it

Author

Commented:
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"
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK what happens when you call the code you had yourself?
CERTIFIED EXPERT
Top Expert 2016

Commented:
btw if you put your file in /WEB-INF/classes the code i posted should work

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
That can't be done - if you change the folder, you must change the code.

Author

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
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?

Author

Commented:
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");) ?

Author

Commented:
btw, i also got the null when i use your code
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Yeah, thanks CEHJ, i got it
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.