• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing InputStream object to Application scope in servlet and retrieving

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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.

Thanks in advance.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot store and save the stream. Obtain it fresh each time.
 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bibeault,

Thanks for your reply. Is there any partcular basic reason that why it cant be done ?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."

Bill
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you hope to gain by doing this?
 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William and Bear for your replies.

Bear,
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.
 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,
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 ???
 
Saloon Keeper
Posts: 7590
177
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A byte[] large enough to contain the entire image contents would be the obvious choice.
 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya.... where was my mind ???

any how, Thanks Tim.



One more thing, should i maintain this byte[] by myself in the application scope or any other way of implementation is there (like server side caching) for doing this ???
 
Tim Moores
Saloon Keeper
Posts: 7590
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you mean by "application scope", but your application *is* the server, is it not? So what's the difference between "in your application" and "on the server side"?
 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By application scope, i meant the ServletContext object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic