struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Max Cooper" <>
Subject Re: image question
Date Wed, 23 Jul 2003 09:04:35 GMT

Since the browser is going to make a seperate request for the image data,
you really just want to render an img tag with a src URL that can be used to
retrieve the image from the db. You will also need an Action (or a servlet,
for a reason I will explain below) that will get the image from the db and
send the data back to the browser.

So, now you just need something in the ActionForm to identify which image
should be displayed in the main JSP page. This will probably be some kind of
image ID, say 7484 in this case. You want the JSP to render an image tag
with the src set to a URL that will get the right image. It could be
something like this (I haven't tested this, so it might need some debugging
to get it working):

JSP source:
<html:img page="/showImage" paramName="myActionForm"

Rendered HTML:
<img src="/theContextPath/showImage?imageId=7484">

Then you could create a servlet that would display images from the database.
Something like this:

public class ShowImageServlet extends HttpServlet {
   protected void doGet(
      HttpServletRequest request,
      HttpServletResponse response
   ) throws ServletException, IOException {
      // get the image ID that is needed from the request
      int imageId = Integer.parseInt(request.getParameter("imageId"));

      String contentType;
      // TODO: read the content type for the image into the contentType
      // I suggest saving the content type in the db with the image

      byte[] data;
      // TODO: assign the data variable to a byte array that contain the
image data
      ServletOutputStream ostream = response.getOutputStream();

   protected long getLastModified(HttpServletRequest request) {

      // get the image ID that is needed from the request
      int imageId = Integer.parseInt(request.getParameter("imageId"));

      long lastModified;
      // TODO: get the last modified date (as a long) for this image from
the db
      // I suggest saving a last modified date with the image in the db
      return lastModified;

You could use an Action instead of a servlet for this, but we decided to use
a servlet on our project because we have caching turned off for our Struts
Actions. This prevented the images from being cached, significantly
affecting performance and putting extra load on the server. By implementing
the getLastModified() method in our servlet (which I don't even think is
possible with an action), we got faster performance and proper caching
behavior (the browser loaded the new image if and when it changed).

Then map your servlet to the /showImage path in web.xml so the app server
will know to use the servlet to process such requests.



Some additional notes:

1) This same basic design can be used for other kinds of binary data, like
PDF files, etc. For this reason, it may be useful to generalize the servlet
name, path, etc. to be clear that other kinds of data can be referenced and
retrieved by this subsystem rather than just images. We used the term
"resource" instead of "image" on our project because we used the same
subsystem to get both images and documents.

2) Some developers may view storing the images in the db as overkill,
inefficient, etc. But, it has worked out great in my experience. One
alternative is to save the files in a filesystem, but doing so has some
significant disadvantages. One is that you need to have a shared filesystem
for all of your servers, or have copies of the files, etc. That is a big
pain during development, and there is overhead in setting up where the image
files are on a particular server. And you need to backup the image data in
addition to the database, etc. Using a filesystem also weakens the
relationship between images and the reference to those images when compared
to using the database to store the data and references. That is all a big
pain -- putting the image data in the database frees you from all of the
problems of using a filesystem. I like storing this kind of data in the
database, and I believe that a good implementation can be just as fast as
storing the images on a filesystem.

3) I am not sure if any such projects exist, but this would be a cool area
of focus for an open source project. I think many people have implemented
something like I describe above, which is duplicated effort. A cool project
could be created that included a JSP taglib to render image and link tags
(for PDFs, etc.), and a servlet that could be used to serve those resources.
The project could implement advanced caching features to allay performance
concerns, such as using a local filesystem to store the images that have
already been pulled from the db, etc. The implementation I described above
requires the knowledge about what the URL to load images should look like in
many different places -- an open source project that addressed this stuff
could hide all of those details, reducing the level of coupling between
different parts of an application. Since the details of loading the data
from the db would likely be different from project to project, an
application developer would need to configure (servlet mapping, db tables,
columns) and/or implement an interface that the servlet could use to access
information about the resources (load by id, get content type, modification
date, binary data). The open source project would need to define these
configuration points and interfaces.


----- Original Message ----- 
From: "Burc D. Gunes" <>
To: <>
Sent: Tuesday, July 22, 2003 9:16 PM
Subject: image question

> Hi,
> I am a newbie to Struts. I made a search on the web, but could not find an
> answer to my question. I was wondering if anyone can help me?
> Currently I have a web application that stores some info about products in
> database. When the users look up a product, I retrieve the information
> the database, populate the ActionForm using the set methods and the
> information displays on the jsp with the struts tag libraries. Now I would
> like to display images of the products as well. I managed to upload the
> images to the database, but I have problems while I am retrieving them at
> the jsp pages.
> I can retrieve the images from the database as a blob object, create a
> javax.swing.ImageIcon object out of the blob object and assign the
> object to the private field of the product ActionForm using set method.
> Unfortunately, I couldn't get this ImageIcon to display on the jsp. Does
> anyone know how can I display this object or should I abandon this method
> and look for a different way of displaying the image once it is retrieved
> from the database. My problem is that I would like to present all of the
> information about the product (ie. product name, description, image, etc)
> the jsp page at the same time, not just the image of the product.
> Thanks for your help,
> Burc
> _________________________________________________________________
> The new MSN 8: advanced junk mail protection and 2 months FREE*
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message