5

Is it possible to find out deployment path from JSF application?

One more question (if the answer for the former question is true): is it legitimate to store some uploaded files there?

Roman
  • 64,384
  • 92
  • 238
  • 332

1 Answers1

11

Is it possible to find out deployment path from JSF application?

Not directly, you'd need to grab the ServletContext from the ExternalContext as follows:

ServletContext servletContext = (ServletContext) externalContext.getContext();

It in turn offers the well-known and dubious getRealPath() method.

String webContentRoot = servletContext.getRealPath("/");

If you're already on JSF 2.0, you could use ExternalContext#getRealPath() instead.


One more question (if the answer for the former question is true): is it legitimate to store some uploaded files there?

You can do so. You only need to take into account that they all get lost whenever you redeploy the application, for the very simple reason that those files are not contained in the original WAR.

If you'd like to have a bit more permanent storage, consider storing it in a fixed path outside the webapplication, e.g. /var/webapp/upload or so. An alternative is storing them in a database, this would increase the portability, but remember to store some metadata (name, content type, last modified timestamp, etc) along the file content as well so that you have something to index/search/cache on.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • actually I need to store uploaded images somewhere. As I found, it's a good practice to store image location in a DB and the image itself somewhere in a file system. I wanted to provide 2 options: somewhere outside app, as you've suggested (implemented already), and under deployment path. But I didn't know that they will be cleared after redeployment so I'll probably abandon this idea. Thanks! – Roman Aug 09 '10 at 15:59
  • Yes, you can also do so. Searching is a lot easier with SQL. Although a purist would say "no" against storing binary data in a DB, it *actually* doesn't harm. It's just .. useless. You can't index/search in binary data. But it's definitely more portable than storing data+metadata on different places. Only when it's a networked DB, it might be performancewise slower than when on same local disk as where the webserver runs. – BalusC Aug 09 '10 at 16:02
  • the main argument that when images stored in a FS I can review them and when they are stored in a DB I need some app for reviewing. But thanks for another point. – Roman Aug 09 '10 at 16:07