struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Max Cooper" <...@maxcooper.com>
Subject Re: img store/retrieve
Date Fri, 06 Dec 2002 12:59:17 GMT
Create a servlet to serve the images from the database. That way you can
implement getLastModified() to help the browser cache the image and reduce
the load on your server. Map it to some url like /dbimage/* or something.

When you write the page that is going to reference the image, have it write
out something like this, with the id (32438 in the examples) in the URI:

<img src="(contextPath)/dbimage/32438">

or

<!-- assuming you will know what kind of image it is when you render the
HTML page
       (which may be a bad assumption, and thus this won't work for your
situation) -->
<img src="(contextPath)/dbimage/32438.gif">
<img src="(contextPath)/dbimage/32438.jpg">
<img src="(contextPath)/dbimage/32438.png">

or

<!-- in this case, just map /showImage to your servlet -->
<img src="(contextPath)/showImage?id=32438">

And then the servlet that handles the dbimage requests will dig the id out
of the request, load that image data from the database (the content type and
bytes) and send the information to the browser in the response. This works
best if you have some kind of Image_Resource (images only) or
Binary_Resource (all kinds of binary files -- images, PDFs, etc.) table in
the db that is shared by all the tables that you want to store this kind of
info for. Each table that used such resources would hold ids to rows in this
shared table, or perhaps you'll have a join table to map resources to rows
in some table for a many-to-many relationship. That allows you to use one
servlet to handle all image or binary data requests.

Here's a doGet() method to send the data back in the response:

   protected void doGet(
      HttpServletRequest request,
      HttpServletResponse response
   ) throws ServletException, IOException {
      response.setContentType(
         /* tell the browser what kind of image or data you are sending */
      );
      byte data[] = /* fill this array with the image data from the db */;
      response.setContentLength(data.length);
      ServletOutputStream ostream = response.getOutputStream();
      ostream.write(data);
      ostream.flush();
   }

To further increase performance, you could have the servlet cache the image
data to a temp area on the disk and serve the data from there. If it doesn't
find the image in the cache, get it from the db, write it to the disk and
then... If it does find the image in the cache (or you just put it there),
it could load the data from the file and send it back (probably faster to
just send the data you pulled from the db if the image wasn't already in the
cache), or if the cached file can be served directly by the server, you
could just forward the request to the on-disk location. Maybe there is even
some way to take the servlet out of the loop if you already have the file in
the disk cache (catch 404 errors for that area and send them to the servlet
to get the data and reset the reponse code to 200). Watch out for images
that need security (graphs of sales data or employee performance data, for
instance), images that might become stale in the disk cache, files like JSPs
or PHP scripts that a user may upload and then exploit the disk cache to
execute on your server (BIG security hole), or that you don't run out of
disk space trying to write a file to the disk. On second thought, perhaps
you don't need disk caching. ;-)

I like having files and things that people upload in the database. It makes
the data easier to manage, since you need to backup or share the db anyway.
This avoids having to backup and possibly share a filesystem among multiple
servers. With the getLastModified() method and/or some on-disk caching, you
can increase the performance of this otherwise slow method of storage,
hopefully to an acceptable level for your application and load requirements.

-Max

----- Original Message -----
From: "Gemes Tibor" <gemes@regens.hu>
To: <struts-user@jakarta.apache.org>
Sent: Thursday, December 05, 2002 4:37 AM
Subject: img store/retrieve


I think this is a bit OT on this list.

I have to store uploaded images into the RDBMS, and then later retrieve them
on request.

This is a simple CRUD page, but the entity has an image property. I
retrieve
the View Object from the RDBMS and populate the ActionForm from it.

I think that the image should not be stored in the VO. Am I right?

Where should I store the image if I have to display it on the page? I cannot
store in request, as the browser creates a new request for each img on the
page afaik. I'm not keen on storing it in the session since I prefer to
store
small amount of data in session scope.

Do I have to create a servlet which sets the content-type depending the img
type, and writes the bytestream back to the user agent? If so how am I
supposed to call this servlet from <html:img> ?

What is the best practice implementing this? Or the suggested one? Or at
least
the least painful one?

TIA,

Tib



--
To unsubscribe, e-mail:
<mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>





--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message