Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cache file(pdf) in user's computer using Servlet-Cookie, or Session

 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I save a file (pdf) in the cache of a browser (IE 6) in a user's computer? I am using Servlet and Tomcat 4.1.24. PDF file is sitting in server (remote computer) under a URL space.
The idea is to NOT to download the pdf file on user's computer (from the remote server) if the user has a cached copy of the pdf file and pdf file in the server is the same as the pdf file in the browser. If cached pdf is different from pdf in browser, or, if the user does not have that pdf file in cache, then download pdf file from the server.
Can I come up with a way to use Cookie class? Cookie class does not have an API which will let me save a File/Object in it. I could have saved the pdf file in the Cookie with 999999999 setMaxAge. Is storing that pdf file in session the only choice? I do not see session serving my purpose - session should not be valid for a longgg duration e.g. week or month.
Also, how would I create a file given a URL to that file.
URL fileInServer = new URL("http://www.myDomain.com/myWebApp/PDF/my.pdf");
URLConnection conn = fileInServer.openConnection();
Does getContent() help and what are the steps after that?
Any help with the source code will be appreciated.
Thanks,
Sam
sgho@aol.com
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cookies are strictly limited to storing text, and not very much text at that. What a browser caches is very much up to the caching policy the user sets.
Are you generating this PDF file on the fly?
Do you want the PDF file to be served by a servlet or the normal web server?
Bill
 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am NOT creating this PDF file on the fly. This pdf file is sitting in the server (remote machine) under URL e.g. http://10.10.10.xxx:8081/myApp/PDF/my.pdf
I need to check if this pdf file is in the cache in user's computer (differnt machine than the server) and if the cached pdf is the same as the pdf in the server. If that file is not in the cache, or if they are not the same, then, I need to save the pdf file from the server into user's browser cache.
I am writing all that code in Servlet. I can save the file in session. I do NOT know how else can I cache the file, other than saving it in session. I can not think of any setHeader() which will let me save a file in user's browser cache.
Also, how would I create a file, given a URL to that file.
URL fileInServer = new URL("http://www.myDomain.com/myWebApp/PDF/my.pdf");
URLConnection conn = fileInServer.openConnection();
Does getContent() help and what are the steps after that?
Thanks,
Sam
sgho@aol.com
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't think of any way for you to determine if the file is already in the browser cache or to control whether or not the browser requests the file from the server. From the server side, you can only send a response, what the browser does with it is up to processes on the browser.
As far as figuring out a server file path from a URL, look at the getRealPath method of ServletContext.
Bill
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't overriding the servlet's getLastModified() method solve this problem? The browser won't download the file again if the last-modify date hasn't changed since the last download.
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:
Wouldn't overriding the servlet's getLastModified() method solve this problem? The browser won't download the file again if the last-modify date hasn't changed since the last download.

Then I think pdf file cant be accessed directly like domainname/file.pdf
It has to route through servlet.
and i dont think they are cached.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic