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 Avoiding the page to get refreshed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Avoiding the page to get refreshed" Watch "Avoiding the page to get refreshed" New topic
Author

Avoiding the page to get refreshed

aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
Hi all,
How to avoid user for the page getting refreshed when ever the user tries
to press Refresh button or press F5
I know an idea by doing it through JavaScript is there any other way
Regards,
aakash
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Out of interest, why would you want to do this? If you are trying to stop data from being resubmitted to the server, then there is a pattern using tokens that you can apply...
Simon
Eric Waite
Greenhorn

Joined: Mar 31, 2003
Posts: 12
I am having the same sort of issue, I have a reports section of my app, and I want to prevent the user from submitting the report request multiple times, holding down f5. Maybe there is something missing I can do from the server side...
The report sometimes takes 1-2 minutes to generate.
Holding down f5 floods the database with requests and grids the app to a halt.
i know I could just trap from javascript but I imagine there is a better way.
- Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

Here's one technique to detect refreshes of an operation:
1) When the JSP page is executing, generate a unique token value
2) Add it as a hidden control to the form whose submission triggers the operation
3) Add it to the session as well
4) When the form's action is submitted, check that the hidden value matches that on the session and refuse to process if not
5) Remove the session token
This way, the initial submission will succeed while subsequent submissions as the result of refreshes will be blocked.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

P.S. Be sure to appropriately synchronize access to the session token.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by Eric Waite:

i know I could just trap from javascript but I imagine there is a better way.
- Eric

That's interesting..how do you trap it from javascript? Is there a generic way of trapping all the ways a page can be refreshed such as F5, Ctrl-R, right click and choose, press Reload button.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Eric Waite
Greenhorn

Joined: Mar 31, 2003
Posts: 12
Thanks everyone, I will try the token idea and let you know how it works out:
On the javascript issue:
I haven't tried it, what I guess I meant to say was, while searching I have found ways to trap functions via javascript:
Here's a snippet: from :
http://www.faqts.com/knowledge_base/view.phtml/aid/1502/fid/145

document.onkeydown = function () {
alert(event.keyCode);
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
return false;
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

Be aware that Javascript event handling is very browser dependent. And, unless you open your own window without it, gives you no control over the brower's reload/refresh button.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

thanks Eric and Bear.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
If the processing takes a minute or two, I would forward the user to a "Wait Page" saying something like "Please wait while the data is being retrieved". This page would have a refresh tag that will refresh ever X seconds to see if the data is available. Once it is available, it forwards the user to the display page so he/she can see the results. Keeping the user waiting for any length of time waiting for a web page to load is only asking for them to hit refresh.
I'm actually in the middle of rewriting some code I have to do exactly this. It is currently done in Struts/Tiles and works fine but sometimes hte user has to wait up to a minute for the data to be retrieved. Now, the user immediately sees a wait page and knows we're working on it. They will see the display page once it's done. If the user refreshes the wait page it's no big deal since that's what the wait page does anyway.
Brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Originally posted by Eric Waite:
Thanks everyone, I will try the token idea and let you know how it works out:
On the javascript issue:
I haven't tried it, what I guess I meant to say was, while searching I have found ways to trap functions via javascript:
Here's a snippet: from :
http://www.faqts.com/knowledge_base/view.phtml/aid/1502/fid/145
}

Hope you only want it to work with IE browsers.......
Eric Waite
Greenhorn

Joined: Mar 31, 2003
Posts: 12
Thanks Brian and Bear for the replys.
I am a little confused about both:
( these are probably more jsp-STRUTS related)
With the token issue:
I am linking to a jsp report off of an applet, and lets say instead of a token I wanted to only allow a refresh after 2 minutes. In my action class that is sent the second time, how to I cancel current processing. You generally need to forward the page on all page requests? Where would I forward to?
With the intermediate page:
Lets say i make call to my mapper class that does that database retrieval, since that is what is taking so long... where would I make that call and still be able to forward to an intermediate page?
- Thanks for all the help
Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Avoiding the page to get refreshed
 
Similar Threads
JSP and drop-down boxes
populating one dropdown box depending on the value of other dropdown box
How do you get new text to appear each time
Need to avoid openging of page in a new window in IE
How to refresh my page?