I have a query here. Actually my requirement here is to fetch a image file from a physical location outside of a WAR file and show it in the jsp. the problem here is that the image will change frequently based on business policy changes. So what i thought was to write a servlet and bind it to the src attribute of the img tag (as below).
Servlet Code :
First time the image is coming fine, but second time when the inputstream is retrieved from application scope i.e. Servlet Context object, i m getting IO exception
"java.io.IOException: The handle is invalid"
Can any one help me here ? First of all my approach is correct or not. If not, is there any other easy and better way to do this ?
I went with this approach just because that this particular image will change frequently, and i dont want to deploy the entire WAR every time.
For one thing, once the stream has been read, it is "positioned" at the end of the stream so you can't read from it again.
As a general Java pointer, you should be very very careful about keeping references to objects which are tied to system resources - in your case, the stream was tied to an open file. If you don't properly dispose of such things, for example by closing the stream, you get mysterious errors - such as your "handle is invalid'.
Beware of tricks that you think might save a microsecond or two - "premature optimization is the root of all evil."
The problem here is that this particular image will keep on changing, so i dont want to bundle it with the WAR or EAR. Since that will need a deployment every time the image changes based on user requirements. so i thought of to put the image a physical location in the server outside of the WAR/EAR and pick it up from there.
But every time that particular page (in which yhat image is used) loads the physical location at that server will be hit and the image file will be picked. In order to avoid this i thought that i might get the file stream from the physical location only for the first request and then will add it to application scope and for every further request i will pick it up from the application scope stream object.
But as William pointed out a Stream once read cant be read back again. So, Is there any other way of doing this ?
P.S. If i achieve this it will save a hell lot of time for our team which is involved in unnecessary deployments just for a single image change.
Joined: May 06, 2010
Long long ago Somewhere i have read that putting a image outside of a WAR/EAR is not a good idea, that too when the image is a frequently used one.
And, Yes, this image in my requirement is a frequently used one, its almost in the home page of our site.
So, how exactly do i do the caching? i dont want to hit the physical location every time, since the image is a frequently used one.
For the very first hit after deploying, i want to pick the image from the server and then cache it for further hits. I dont want to hit the physical location outside of the server for every request.
Can you help me out here with exactly how to do the caching ???