File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Storing InputStream object to Application scope in servlet and retrieving Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Storing InputStream object to Application scope in servlet and retrieving" Watch "Storing InputStream object to Application scope in servlet and retrieving" New topic
Author

Storing InputStream object to Application scope in servlet and retrieving

Vinodh Sa
Ranch Hand

Joined: May 06, 2010
Posts: 55
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.


Thanks, Vinodh
[Servlet tutorial]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

You cannot store and save the stream. Obtain it fresh each time.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vinodh Sa
Ranch Hand

Joined: May 06, 2010
Posts: 55
Hi Bibeault,

Thanks for your reply. Is there any partcular basic reason that why it cant be done ?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

What did you hope to gain by doing this?
Vinodh Sa
Ranch Hand

Joined: May 06, 2010
Posts: 55
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

Joined: May 06, 2010
Posts: 55
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 ???
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
A byte[] large enough to contain the entire image contents would be the obvious choice.
Vinodh Sa
Ranch Hand

Joined: May 06, 2010
Posts: 55
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: May 06, 2010
Posts: 55
By application scope, i meant the ServletContext object.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Storing InputStream object to Application scope in servlet and retrieving