Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting absolute path jsf

Tags:

java

jsf

facelets

I want to get the absolute path of my app and I'm using the code below:

String pathToSave = FacesContext.getCurrentInstance()
                .getExternalContext().getRealPath("/");

the result is:

/home/ronaldo/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Odontonew/

But I'm waiting for something like: /home/ronaldo/workspace/Odontonew/

What is wrong ?

like image 755
Ronaldo Lanhellas Avatar asked Aug 27 '26 09:08

Ronaldo Lanhellas


1 Answers

ExternalContext#getRealPath() returns the path relative to where the webapp is been deployed, not where the webapp is been developed orso, as you incorrectly seemed to expect.

But, the variable name pathToSave indicates a much bigger problem: you seem to intend to save files in there. This is a bad idea for the reasons mentioned in the following answer: Uploaded image only available after refreshing the page. In a nutshell, save them to a fixed local disk file system path instead. Note that some servers offer ways to make this step easier configurable, e.g. JBoss.

Just stop using getRealPath(). You never need it in real world. In the 10 years I developed Java web applications (it's under the covers coming from ServletContext#getRealPath()), that method was never been useful for anything. Just ignore that method altogether.

See also:

  • What does servletcontext.getRealPath("/") mean and when should I use it
like image 193
BalusC Avatar answered Aug 28 '26 22:08

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!