Meaningless Drivel is fun!
The moose likes Servlets and the fly likes serving HTML with images from the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "serving HTML with images from the database" Watch "serving HTML with images from the database" New topic

serving HTML with images from the database

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
This may look long, but it's easy to understand--I'm just trying to give all the details you might want: In our servlet/JSP site, a user can generate a report that may or may not contain images. The servlet just sends the user to a regular HTML page. The HTML file and images it may have are stored in directories on the Tomcat server. But now we have a problem...

We're moving to 2 standalone Tomcat machines with a load balancer in front, and so now we want to move any of this stuff that was stored on the webserver into the database. If the HTML text for the report has not yet been generated for that day, I can see my web app going to the database to pull out that HTML, and if there are any images, it could pull those out of the database, but does it get those to the user's web browser?

Currently, when this report loads in the browser, we do a sendRedirect to the file, and the URL displays the full path to the file (and in the HTML code, there is of course a relative link to the image file). But now, these are supposed to be pulled out of the database...are we going to have to pull them out of the database and then write them out to file on the webserver (since they might not exist on that webserver), or is there some other/better way to display them to the user?

[ December 10, 2004: Message edited by: Stephen Huey ]
Ajith Anand
Ranch Hand

Joined: Aug 30, 2004
Posts: 40
Inside the HTML you could do something like this

Here the SRC points to a servlet. It goes to the servlet with the appropriate details which helps the servlet retrieve the image from the database.

you can have DAOs or JDBC to access the database retrieve the BLOBs using the information provided in the query string to QUERY the database.
Once you have the image BLOB retrieved from the database , you need to set the content-type of the response to the appropriate image type eg: "image/jpg" . Write the content of the blob to the response stream of the servlet....

LXI Technologies P Ltd
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Sweet! Next one's on me, man...
I agree. Here's the link:
subject: serving HTML with images from the database
It's not a secret anymore!