


Several methods for JSP to prevent repeated submission of data after page refresh
Dec 06, 2016 pm 01:52 PMThis article mainly introduces the solutions on how to prevent webpages from refreshing and resubmitting and how to prevent backing off. The details are as follows:
Disable the submit button after submitting (most people do this)
If the customer presses F5 after submitting What should I do if I refresh?
Use Session
Before the submitted page is processed by the database:
if session("ok")=true then response.write "錯(cuò)誤,正在提交" response.end end if
After the data is processed, modify session("ok")=false.
Redirect to another page immediately after successful data processing
Refreshing after operation is indeed a problem. You can use jump page or close this page. If it is controlled by parameter data conditions, it should be easy. You can directly Modify the value of window.location and change all the parameters, and you're almost done.
Disadvantages: Simply applying Response.Redirect will no longer work, because when the user goes from one page to another, we all have to clear location.history with client code. Note that this method clears the last access history record, not all access records. Click the back button, and then click the back button again. You can see that the page before this page is opened now! (Of course, this is if JavaScript is enabled on your client.)
What if the customer presses back?
Prevent the webpage from going back - disable caching
When we add the database, if we allow the backing and the page happens to be refreshed, the adding operation will be performed again. Undoubtedly, this is not what we need, like many prohibitions on the Internet. The cached code is sometimes unreliable. In this case, you only need to add it to the operation page. Specify the new page to be directed in the web page, and then click Back to see if it will not return to the previous operation page. Yes, this history has actually been deleted
ASP:
Response.Buffer = True Response.ExpiresAbsolute = Now() - 1 Response.Expires = 0 Response.CacheControl = "no-cache"
ASP.NET:
Response.Buffer=true; Response.ExpiresAbsolute=DateTime.Now.AddSeconds(-1); Response.Expires=0; Response.CacheControl="no-cache";
How can I "disable" the browser's back button? Or “How can I prevent users from clicking the back button to return to a previously viewed page?”
Unfortunately, we can’t disable the browser’s back button.
Prevent the web page from backing up - open a new window
Use window.open to pop up the form page, click submit to close the page; the ASP page for processing the submission also uses pop-up, set the target of the form, click window.open(" XXX.asp", "_blank"), and then use JS to submit the form. After completion, window.close();
Simply put, when submitting the form, a new window will pop up and this window will be closed. How to go back to a window opened by window.open()? Where can you retreat to?
Haha, it’s a lot of nonsense, do you know how to deal with it? Use a mix of client-side and server-side scripts.
jsp duplicate submission problem
I looked online and there are several methods:
1 Add this code in the HEAD area of ??your form page:
<META HTTP-EQUIV="pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> <META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
2 Generate a token and save it in the user In the session, add a hidden field to the form to display the value of the token. After the form is submitted, a new token is generated. Compare the token submitted by the user with the token in the session. If they are the same, submit again
3 Use Response.Redirect("selfPage") statement in the code of your server-side control. But most people don't use this method.
There are many more methods. . .
4,
<input type="button" value="提交" onclick="this.disabled=true;this.form.submit()">
5 Add a hidden field in the FORM form of the JSP page
<input type="hidden" name="url"value=<%=request.getRequestURL()%>>
Add the following statement in your serverlet (Java code)
String url=request.getParameter("url"); response.sendRedirect(url);
me This method is generally used to return to the JSP page. I don’t quite understand what you mean by repeated refresh.
6 Ajax submission without refresh
7 In web development, how to prevent the browser’s refresh key from causing repeated submission of system operations
What's the solution? Redirection can solve the problem of repeated submission of data caused by page refresh. We can naturally use redirection to solve this problem. But in the struts action mapping.findword(); when jumping, the default is to find the page to jump in the project folder. How to solve this situation?
Modify the struts-config.xml file. There is a redirect attribute in the action. The default in struts is false. Add this attribute, change it to true, and write the absolute or relative value of the page to be jumped in the forword. Just change the address
as follows:
<action-mappings> <action attribute="newsActionForm" name="newsActionForm" input="/addnews.jsp" path="/newsAction" parameter="method" scope="request" type="com.yongtree.news.action.NewsAction"> <forward name="list" path="/listnews.jsp" redirect="true"></forward> <forward name="error" path="/addnews.jsp"></forward> </action> </action-mappings>
Problems related to browsers
The back button of the browser allows us to easily return to previously visited pages, which is undoubtedly very useful. But sometimes we have to turn off this feature to prevent users from disrupting the scheduled page access sequence. This article introduces various solutions for disabling the browser back button that can be found on the Internet, analyzing their respective advantages, disadvantages, and applicable situations.
1. Overview
曾經(jīng)有許多人問起,“怎樣才能‘禁用'瀏覽器的后退按鈕?”,或者“怎樣才能防止用戶點(diǎn)擊后退按鈕返回以前瀏覽過的頁面?”在ASP論壇上,這個(gè)問題也是問得最多的問題之一。遺憾的是,答案非常簡(jiǎn)單:我們無法禁用瀏覽器的后退按鈕。
起先我對(duì)于居然有人想要禁用瀏覽器的后退按鈕感到不可思議。后來,看到竟然有那么多的人想要禁用這個(gè)后退按鈕,我也就釋然(想要禁用的只有后退按鈕,不包括瀏覽器的前進(jìn)按鈕)。因?yàn)樵谀J(rèn)情況下,用戶提交表單之后可以通過后退按鈕返回表單頁面(而不是使用“編輯”按鈕?。?,然后再次編輯并提交表單向數(shù)據(jù)庫(kù)插入新的記錄。這是我們不愿看到的。
因此我就決定要找出避免出現(xiàn)這種情況的方法。我訪問了許多網(wǎng)站,參考了這些網(wǎng)站所介紹的各種實(shí)現(xiàn)方法。如果你經(jīng)常訪問ASP編程網(wǎng)站,本文所介紹的部分內(nèi)容你可能已經(jīng)見到過。本文的任務(wù)是把各種可能的方法都介紹給大家,然后找出最好的方法!
二、禁止緩存
在我找到的許多方案中,其中有一種建議禁止頁面緩存。具體是使用服務(wù)器端腳本,如下所示:
<% Response.Buffer = True Response.ExpiresAbsolute = Now() - 1 Response.Expires = 0 Response.CacheControl = "no-cache" %>
這種方法非常有效!它強(qiáng)制瀏覽器重新訪問服務(wù)器下載頁面,而不是從緩存讀取頁面。使用這種方法時(shí),編程者的主要任務(wù)是創(chuàng)建一個(gè)會(huì)話級(jí)的變量,通過這個(gè)變量確定用戶是否仍舊可以查看那個(gè)不適合通過后退按鈕訪問的頁面。由于瀏覽器不再緩存這個(gè)頁面,當(dāng)用戶點(diǎn)擊后退按鈕時(shí)瀏覽器將重新下載該頁面,此時(shí)程序就可以檢查那個(gè)會(huì)話變量,看看是否應(yīng)該允許用戶打開這個(gè)頁面。
例如,假設(shè)我們有如下表單:
<% Response.Buffer = True Response.ExpiresAbsolute = Now() - 1 Response.Expires = 0 Response.CacheControl = "no-cache" If Len(Session("FirstTimeToPage")) > 0 then &single; 用戶已經(jīng)訪問過當(dāng)前頁面,現(xiàn)在是再次返回訪問。 &single; 清除會(huì)話變量,將用戶重定向到登錄頁面。 Session("FirstTimeToPage") = "" Response.Redirect "/Bar.asp" Response.End End If &single; 如果程序運(yùn)行到這里,說明用戶能夠查看當(dāng)前頁面 &single; 以下開始創(chuàng)建表單 %> <form method=post action="SomePage.asp"> <input type=submit> </form>
我們借助會(huì)話變量FirstTimeToPage檢查用戶是否是第一次訪問當(dāng)前頁面。如果不是第一次(即Session("FirstTimeToPage")包含某個(gè)值),那么我們就清除會(huì)話變量的值,然后把用戶重新定向到一個(gè)開始頁面。這樣,當(dāng)表單提交時(shí)(此時(shí)SompePage.asp被打開),我們必須賦予FirstTimeToPage一個(gè)值。即,在SomePage.asp中我們需要加上下面的代碼:
Session("FirstTimeToPage") = "NO"
這樣,已經(jīng)打開SomePage.asp的用戶如果點(diǎn)擊后退按鈕,瀏覽器將重新請(qǐng)求服務(wù)器下載頁面,服務(wù)器檢查到Session("FirstTimeToPage")包含了一個(gè)值,于是就清除Session("FirstTimeToPage"),并把用戶重定向到其他頁面。當(dāng)然,所有這一切都需要用戶啟用了Cookie,否則會(huì)話變量將是無效的。
另外,我們也可以用客戶端代碼使瀏覽器不再緩存Web頁面:
<html> <head> <meta http-equiv="Expires" CONTENT="0"> <meta http-equiv="Cache-Control" CONTENT="no-cache"> <meta http-equiv="Pragma" CONTENT="no-cache"> </head>
如果使用上面的方法強(qiáng)制瀏覽器不再緩存Web頁面,必須注意以下幾點(diǎn):
只有在使用安全連接時(shí)“Pragma: no-cache”才防止瀏覽器緩存頁面。對(duì)于不受安全保護(hù)的頁面,“Pragma: no-cache”被視為與“Expires: -1”相同,此時(shí)瀏覽器仍舊緩存頁面,但把頁面標(biāo)記為立即過期。在IE 4或5中,“Cache-Control”META HTTP-EQUIV標(biāo)記將被忽略,不起作用。
在實(shí)際應(yīng)用中我們可以加上所有這些代碼。然而,由于這種方法不能適用于所有的瀏覽器,所以是不推薦使用的。但如果是在Intranet環(huán)境下,管理員可以控制用戶使用哪種瀏覽器,我想還是有人會(huì)使用這種方法。
三、其他方法
接下來我們要討論的方法以后退按鈕本身為中心,而不是瀏覽器緩存。這兒有一篇文章Rewiring the Back Button很值得參考。不過我注意到,如果使用這種方法,雖然用戶點(diǎn)擊一下后退按鈕時(shí)他不會(huì)看到以前輸入數(shù)據(jù)的頁面,但只要點(diǎn)擊兩次就可以,這可不是我們希望的效果,因?yàn)楹芏鄷r(shí)候,固執(zhí)的用戶總是能夠找到繞過預(yù)防措施的辦法。
另外一種禁用后退按鈕的辦法是用客戶端JavaScript打開一個(gè)沒有工具條的窗口,這使得用戶很難返回前一頁面,但不是不可能。一種更安全但相當(dāng)惱人的方法是,當(dāng)表單提交時(shí)打開一個(gè)新的窗口,與此同時(shí)關(guān)閉表單所在的窗口。但我覺得這種方法不值得認(rèn)真考慮,因?yàn)槲覀兛偛荒茏層脩裘刻峤灰粋€(gè)表單就打開一個(gè)新窗口。
那么,在那個(gè)我們不想讓用戶返回的頁面是否也可以加入JavaScript代碼呢?在這個(gè)頁面中加入的JavaScript代碼可用來產(chǎn)生點(diǎn)擊前進(jìn)按鈕的效果,這樣也就抵消了用戶點(diǎn)擊后退按鈕所產(chǎn)生的動(dòng)作。用于實(shí)現(xiàn)該功能的JavaScript代碼如下所示:
<script language="JavaScript"> <!-- javascript:window.history.forward(1); //--> </script>
同樣地,這種方法雖然有效,但距離“最好的方法”還差得很遠(yuǎn)。后來我又看到有人建議用location.replace從一個(gè)頁面轉(zhuǎn)到另一個(gè)頁面。這種方法的原理是,用新頁面的URL替換當(dāng)前的歷史紀(jì)錄,這樣瀏覽歷史記錄中就只有一個(gè)頁面,后退按鈕永遠(yuǎn)不會(huì)變?yōu)榭捎?。我想這可能正是許多人所尋求的方法,但這種方法仍舊不是任何情況下的最好方法。使用這種方法的實(shí)例如下所示:
<A HREF="PageName.htm" onclick="javascript:location.replace(this.href); event.returnValue=false;">禁止后退到本頁面的鏈接</A>
禁止后退到本頁面的鏈接!
這種方法的缺點(diǎn)在于:簡(jiǎn)單地運(yùn)用Response.Redirect將不再有效,這是因?yàn)槊看斡脩魪囊粋€(gè)頁面轉(zhuǎn)到另一個(gè)頁面,我們都必須用客戶端代碼清除location.history。另外還要注意,這種方法清除的是最后一個(gè)訪問歷史記錄,而不是全部的訪問記錄。
點(diǎn)擊上面的鏈接,你將打開一個(gè)簡(jiǎn)單的HTML頁面。再點(diǎn)擊后退按鈕,你可以看到這時(shí)打開的不是本頁面,而是本頁面之前的頁面!(當(dāng)然,你必須在瀏覽器中啟用了客戶端JavaScript代碼。)
經(jīng)過一番仔細(xì)的尋尋覓覓之后,我發(fā)現(xiàn)仍舊無法找出真正能夠完全禁用瀏覽器后退按鈕的辦法。所有這里介紹的方法都能夠在不同程度上、以不同的方式禁止用戶返回前一頁面,但它們都有各自的局限。由于不存在能夠完全禁用后退按鈕的方法,所以最好的方案應(yīng)該是:混合運(yùn)用客戶端腳本和服務(wù)器端腳本。
<html> <head> <meta http-equiv="Expires" CONTENT="0"> <meta http-equiv="Cache-Control" CONTENT="no-cache"> <meta http-equiv="Pragma" CONTENT="no-cache"> </head>