struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Max Cooper" <>
Subject Re: Image References From JSP Pages inside WEB-INF
Date Sat, 25 Jan 2003 01:03:52 GMT
Remember that requests for images are separate from the requests for pages.
It works kind of like this dialog between the browser and server:

browser: please send /index.html

server: okay, here it is:
<body><img src="/images/bmx_mullet.jpg"></body>

browser: (oh! I need an image, I better ask for it...) please send

server: okay, here it is: 001010100100101010101001010...

Because it works this way, putting images in /WEB-INF will prevent the
server from being able to serve them normally. Unless you have a
well-thought-out reason to put them there, it is probably not a good idea.

If you have images that you want people to be authenticated to see, you can
simply put them in a directory that is protected via container-managed
security, as defined in your web.xml. Then let the server do its job serving
the images. Your images are secured, and no custom servlets or actions are

If the images are files and just need more fine-grained security, a Filter
might be appropriate. However, be careful not to create an image dumping
ground that will end up full of stale images. Backup and sharing among
clustered servers can be an issue also. If an image only needs to be seen
once, writing it to a file might not be a very good idea. A servlet or an
Action to stream the image data back to the user might be more appropriate.
That way you don't have to manage files, which can be problematic in many
ways. For example, if you have an action that shows a page with employee
info including a dynamically-generated graph of their salary history over
time, your browser/server dialog might look like this:

browser: please send /

server: okay, here it is: (server invokes Struts Action to show employee
Employee Number: 100<br>
Name: Kid Rock<br>
Salary History:<br>
<img src="/servlet/employeeSalaryHistory.gif?id=100">

browser: please send /servlet/employeeSalaryHistory.gif?id=100

server: okay, here it is: (server runs servlet mapped to
/servlet/employeeSalaryHistory.gif that dynamically creates an employee
history salary chart image and streams the data back in the response)

Another case where it can be useful to use an action or servlet to stream
the data back is when you allow users to upload images and you want to store
them as BLOBs in the db rather than the filesystem for easy backups,
clusters, or whatever reason. There are no files on the disk, so you need to
write out img tags that will ask your servlet/Action for the image they want
and then have the servlet/Action load the image from the db and stream the
data back.

When streaming data from an action or servlet, be sure to set the MIME type
of the response appropriately. Using a mapping that looks like an image
filename (/something.jpg)can also be helpful to browser when it needs to
figure out what kind of file it is going to get back. However, it probably
wouldn't be a good idea to send an GIF in response to a request for
/servlet/showImage.jpg, so only do that if you know the type. Or make the
mapping /showImage/* and write out img tags with something like
src="/showImage/2259.gif" to match the real type of the file. The "filename"
from the URL can help, but it is most important to call
response.setContentType() with the right MIME type string. Content length
should be set as well.

In the case where it is okay for the browser to cache the images (uploaded
product image that lots of people will look at, etc.), you might be able to
increase page load performance and decrease server load significantly by
creating a servlet that implements the getLastModified() method. That made a
huge difference in page load times on a project I worked on.


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

View raw message