Question

I need to determine if an image exists in a JSP tag so I can display a default if it doesn't exist. What is the best way to access the War root so that I can check to see if the image exists? Or is there a better solution?

Was it helpful?

Solution

The ServletContext method getResource can be used for features like this. If the resource (the image) doesn't exist, this method returns null. In that case, a default image can be displayed.

Note that the URL this method returns is not the URL to use as the "src" attribute in an <img> tag. It is most likely a file: or jar: protocol URL that wouldn't make sense on another host. In this application, its only tested to see if it is null, and not actually by the JSP.

OTHER TIPS

Also note there is no guarantee that the web container has exploded your WAR file, so you should avoid peeking at the filesystem.

Would it be an option to generate the list of available images at deployment time?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top