File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How to stop caching problem in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to stop caching problem in jsp" Watch "How to stop caching problem in jsp" New topic
Author

How to stop caching problem in jsp

kavinkumar Rajendran
Greenhorn

Joined: Jan 10, 2012
Posts: 8
I am using glassfish as server and jsp for sqlserver connectivity .. i ve retieved images from DB to a specific location and try to update it in the page but oly the first time the image loads later the same images get loaded... TEll me how to use Etag and last modified to avoid caching...
Vicky Vijay
Ranch Hand

Joined: Apr 23, 2008
Posts: 125
kavinkumar kumar wrote:I am using glassfish as server and jsp for sqlserver connectivity .. i ve retieved images from DB to a specific location and try to update it in the page but oly the first time the image loads later the same images get loaded... TEll me how to use Etag and last modified to avoidcaching...


Can you elaborate the below,

"i ve retieved images from DB to a specific location"


Are you retrieving the image from DATABASE and placing in a physical repository ???
kavinkumar Rajendran
Greenhorn

Joined: Jan 10, 2012
Posts: 8
Guru Vijay wrote:

Are you retrieving the image from DATABASE and placing in a physical repository ???


I ve retrieved the images from db already and saved them in systems HD , when i ry to upload them onto jsp page oly 1st time the image gets loaded nxt time instead of getting new image from HD it get old image from server cache.. now i want to know how to bypass server cache and to upload image on jsp page tat can be changed dyanmicallly
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and I remember Bear always says here why filter is good!(one of the reason though) :)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Have you looked through the JspFaq and ServletsFaq for information on cache control?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kavinkumar Rajendran
Greenhorn

Joined: Jan 10, 2012
Posts: 8
I tried cache control solutions but none worked.. For me my server s caching the script and img files..any Solution guys..
Daniel Val
Ranch Hand

Joined: Jan 09, 2012
Posts: 44
kavinkumar Rajendran wrote:I tried cache control solutions but none worked.. For me my server s caching the script and img files..any Solution guys..


Hi,

The solution is as follows:

1. Not the jsp is cached - *.jsp url is never cached but the images. The images are the object of separate http requests

2. How you do it

instead of

you put


And generate instead of randomstuffhere some random string. The best for me worked with a java class called RandomGuid which is freely available on the internet.

This used to be a very good approach for the old mobile browser programming back in the days when phones were dumb and were caching everything. Be careful though as your browser sandbox will fill with various images - not that it would be a big issue.

D
kavinkumar Rajendran
Greenhorn

Joined: Jan 10, 2012
Posts: 8
Daniel Val wrote:
kavinkumar Rajendran wrote:I tried cache control solutions but none worked.. For me my server s caching the script and img files..any Solution guys..


Hi,

The solution is as follows:

1. Not the jsp is cached - *.jsp url is never cached but the images. The images are the object of separate http requests

2. How you do it

instead of

you put


And generate instead of randomstuffhere some random string. The best for me worked with a java class called RandomGuid which is freely available on the internet.

This used to be a very good approach for the old mobile browser programming back in the days when phones were dumb and were caching everything. Be careful though as your browser sandbox will fill with various images - not that it would be a big issue.

D



ll try that Daniel ...thanks...
kavinkumar Rajendran
Greenhorn

Joined: Jan 10, 2012
Posts: 8
ll this work ??

<img src="<%=adress%>">

where
String adress="name.jpg?id=123abc";
Daniel Val
Ranch Hand

Joined: Jan 09, 2012
Posts: 44
kavinkumar Rajendran wrote:ll this work ??

<img src="<%=adress%>">

where
String adress="name.jpg?id=123abc";

Please give it a try - should be fine, but where do you take the address from?

Or write a tag that would merely render some random stuff and then use it there. You might need it elsewhere as well...

Personally I avoid at all cost Java code in jsp pages;

Daniel
kavinkumar Rajendran
Greenhorn

Joined: Jan 10, 2012
Posts: 8
UUID idone = UUID.randomUUID();
<img src="1.jpg?id=<%=idone%>" id="i1">

I tried this and also kept entire "1.jpg?id=randvalue" in a string and then executed the jsp the image din change at all...

can you explain the other solution you provided Daniel.. Thanks in advance
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Daniel is very correctly suggesting that you no longer use Java code and Java Scriptlets in JSP pages. That is a practice that has been discredited for 10 years now. Do you not think that you should start using more modern approaches such as the JSTL and EL?

A custom tag that generates a random value (by whatever means) would be one way to implement this. I think that an EL function would be preferable as it is a bit more versatile as it can be used in contexts in which tags are not allowed.

Personally, I'd avoid polluting the page with such implementation details at all and implement a filter that applies the cache control headers to the image requests.
kavinkumar Rajendran
Greenhorn

Joined: Jan 10, 2012
Posts: 8
I dono how to use JSTL or EL .. I Actually finished my entire project the oly problem s that i get the same images always..

If there s a way to break cache in JSTL or EL let me know.. I open to options guys...


And one more thing Guys ..Etags and lastmodified tags can solve the cache problem it seems..so can anyone explain me how to use them...

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

kavinkumar Rajendran wrote:I dono how to use JSTL or EL

And this is an excuse not to learn them?

If you are a novice to JSP, why are you wasting time using technology that's 10 years old? Learn the correct way of coding JSPs now.

I also recommend that novices to JSP read the following articles:
  • The Secret Life of JSPs
  • The Front Man


  • .. I Actually finished my entire project the oly problem s that i get the same images always..

    "oly"? I have no idea what you are trying to say here.

    If there s a way to break cache in JSTL or EL let me know.. I open to options guys...

    Before posting in the forums you should have checked the FAQs. Have you looked through the JspFaq and ServletsFaq for information on cache control?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to stop caching problem in jsp