[thelist] need help loading default image, then changing it with actual image

Hassan Schroeder hassan at webtuitive.com
Tue Nov 15 20:39:02 CST 2005

Chris Johnston wrote:

> First, the specifics: JSP/Servlet app
> I am writing a document versioning application. One of the pages includes
> file information. On this page, I have an icon that changes depending on the
> file type (file extension). Currently, the way that this is implemented is
> that the jsp scriptlet gets the extension of the file and concatenates this
> wil .png and retrieves the appropriate icon. So if the file extension is
> .doc, it would look up the image doc.png.
> However, I can not have icons for every kind of file extension. What I would
> like to do is if the icon does not exist, the page should load up a default
> image. I accomplished this using css by setting a background image, but not
> all the images are the same size, so it overlays the front image with the
> background image. (this looks funny depending on the images)
> I would like to figure out a way of doing this with either JSP and/or css
> instead of JavaScript. However, I am open to all suggestions.

I'd probably do something like, roughly:

ArrayList availableImages = new ArrayList();
  /* populate as desired; could be dynamically loaded from image
   *   directory, or could be static :-)
   * place ArrayList in application scope

  /* in page or request scope */
String imageName;
String extension;
  /* set extension, e.g. */
extension = "doc";

if ( availableImages.contains(extension) ) {
	imageName = extension + ".png";
else {
	imageName = "default.png";

Untested, use at own risk :-)

Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                          dream.  code.

More information about the thelist mailing list