tags:

views:

28

answers:

1

I wrote a little Java servlet that would dynamically generate an image button given parameters including label, height, width and so on, and it would add each new one to a cache. It worked fine, BUT it required a servlet call for every call to display the button or its highlighted version. I dumped the cache and made PNG files of all the buttons used at the moment in the app, added those files to the app so they could be referenced by "/images/button/xyz.png", where the filename is a hash of the input parameters.

That works fine, but I want my original servlet to be called on the occasion when someone has added a new button. I use a custom tag to define these buttons, so the tag handler gets called when the JSP compiles... so I have a point where I can choose to use one of the pre-generated buttons, OR generate a reference to the servlet so it can render the button when it is displayed.

My question is: How can I detect the image is available in pre-rendered for? I'm not sure that a call to create a URL object based off the full path and then doing a call to getResource() is the answer -- I don't want it to load the image at this time, I just need to know if it exists.

Is there some way to create a File object that would take a relative URI and allow me to test for existence?

A: 

I suppose you are talking about the getResource() method on java.lang.Class . This is actually well-suited to the problem since by contract, it will return null if no resource exists with the given path. The resource isn't actually loaded until you use getResourceAsStream() (or similar) so this is not a performance concern.

David Winslow
The issue, I believe, is that the image file is not on the classpath of my taglib. They are deployed to a different jar file. The images are relative to the web root, but even when I pass the fully-qualified URL to getResource (or to File), it isn't found.
Brenda Holloway
Marking answered. This is good information about getResource; it didn't solve my problem, but I am not sure it can be solved in the way I would like it solved.
Brenda Holloway