1

I'll try to be short with the description of my situation:

I'm making a restaurant recommendation web site. I want users to be able to add a new restaurant and upload 1 picture of the restaurant (restaurant's profile picture). That picture will later be displayed when users search for the restaurants. Each time a new restaurant is added I want to create new folder for that restaurant and place the uploaded picture there.

I can upload an image and place on my file system, but when I try to display it it doesn't show.

<img src="\C:\glassfish4\glassfish\domains\domain1\applications\__internal\WebAppName\profilePicture.jpg" />

This is where I was originally placing the images, but they wouldn't show.

If I understood correctly from here that is not a good practice to reference images in this manner because then the browser will be looking for that location in user's machine.

I tried to place the images in:

C:\glassfish4\glassfish\domains\domain1\eclipseApps\WebAppName

because I figured that this is where WebContent folder is (please, correct me if I am wrong), and that this location is accessible with:

<img src="http://localhost:8080/WebAppName/..."/>

but that worked only for a short time. As soon as I redeployed my app the folders in which the pictures were placed had gone away (and they were created).

So my question(s) are:

  1. How and where to place these images, and what should my src attribute look like in an html document (should it be like C:\... or http://localhost/...)?

  2. What are conventions, practices for this, and how is this generally done?

  3. And does redeployment has anything to do with my pictures being gone?

I found this post, but it did not solve my problem.

Note: - I am using glassfish4, and Java Servlets, JSP, JSTL/EL, and generally Java.

Thanks in advance!

Community
  • 1
  • 1
Djole Pi
  • 107
  • 2
  • 14

1 Answers1

0

And does redeployment has anything to do with my pictures being gone?

It does. When you redeployed your application, GAS removed the application directory at ${GAS_INST_DIR}\domains\${YOUR_DOMAIN_NAME}\application{YOUR_APP_BUNDLE_NAME}. Once you decided to store you images there, they are gone after the redeployment.

How and where to place these images

The most straightforward way is to put your files somewhere outside application server folder could be a solution but I would say just half a solution. Let's assume you store your pictures in a local folder /var/application/data. Later you decided to cluster your application. Now you are again in trouble. Each instance has its own /var/application/data directory and as a rule you do not know what node will handle a request for storing an image.

What are conventions, practices for this, and how is this generally done?

I would say it is you who decides what way to go according to the needs of your application. I will list the ways that are the most obvious. All have their own strength and weaknesses.

  1. You can put the images in a local folder. The strong side is simplicity. Once you decide to cluster, you would have to remake this approach. If you go this way the most general approach would be to create a servlet that loads your images and in this case your src= will point to the servlet. Did not find a good example right away, but I think this example will give you an idea how to do it. The only thing I would suggest using finally block or if you use jdk 1.7 the try-with-resources for closing stream. Another thing you will need to pass file name as a parameter to the servlet.

  2. Store images in database. It could be RDBMS or noSql. Down side is that not all RDBMSs work efficiently with binary data. Again src could point to a servlet that loads images from the DB. Here you should design your DB accordingly so you can retrieve images effectively. I would not choose this approach, but this is just a personal opinion. Cannot say how efficient the noSql databases are for storing binary data. You should do it yourself

  3. Consider Webdav. In this case your src attribute will be a link to a resource in webdav server. You can use it in clustered environment, relatively simple implementation.

and what should my src attribute look like in an html document (should it be like C:... or http://localhost/...)?

Depends on the approach you choose. See item 1-3.

Hope that helps.

jjd
  • 2,158
  • 2
  • 18
  • 31