亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 PHP開發(fā) Several methods for JSP to prevent repeated submission of data after page refresh

Several methods for JSP to prevent repeated submission of data after page refresh

Dec 06, 2016 pm 01:52 PM
node

This 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>   
 


Asp.net中防刷新重復(fù)提交、防后退方法

簡(jiǎn)單操作方法防后退和刷新

Page_Load中加入

Response.Cache.SetNoStore();
 
//Session中存儲(chǔ)的變量“IsSubmit”是標(biāo)記是否提交成功的
if (!IsPostBack)
if (Session["IsSubmit"]==null)
Session.Add("IsSubmit",false);
if ((bool)Session["IsSubmit"])
 
{
 
//如果表單數(shù)據(jù)提交成功,就設(shè)“Session["IsSubmit"]”為false
 
Session["IsSubmit"] = false;
 
//顯示提交成功信息
 
TextBox1.Text = " * 提交成功!";
 
}
else
 
{//否則的話(沒有提交,或者是頁面刷新),不顯示任何信息
 
TextBox1.Text = "";
Response.End();
}

提交按鈕中加入

Session["IsSubmit"] = true;
Response.Redirect ("本頁");

? ?

另外:

1、通常應(yīng)該在業(yè)務(wù)層進(jìn)行判斷(唯一性)解決這種問題

2、要在頁面裝載事件寫上??? Response.CacheControl = "no-cache"???? 清除緩存

3、也有人這樣說:我以前也碰到過這樣的問題,是在分步提交中一個(gè)人的簡(jiǎn)歷,在寫完第一個(gè)頁面后跳到第二個(gè)頁面,為了防止用戶用后退返回到第一個(gè)頁面,再重新提交第一個(gè)頁面,我是當(dāng)用戶提交第一次提交第一個(gè)頁面時(shí),把插入數(shù)據(jù)庫(kù)中的記錄的自增長(zhǎng)id號(hào)放到session里,當(dāng)用戶從第二個(gè)頁面返回到第一個(gè)頁面再一次提交該頁面時(shí),我就用session里的值去數(shù)據(jù)庫(kù)查,如果有這個(gè)id就用update語句把第一個(gè)頁面的數(shù)據(jù)寫進(jìn)數(shù)據(jù)庫(kù),如果沒有查到這個(gè)id,就用insert語句。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to delete node in nvm How to delete node in nvm Dec 29, 2022 am 10:07 AM

How to delete node with nvm: 1. Download "nvm-setup.zip" and install it on the C drive; 2. Configure environment variables and check the version number through the "nvm -v" command; 3. Use the "nvm install" command Install node; 4. Delete the installed node through the "nvm uninstall" command.

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

How to do Docker mirroring of Node service? Detailed explanation of extreme optimization How to do Docker mirroring of Node service? Detailed explanation of extreme optimization Oct 19, 2022 pm 07:38 PM

During this period, I was developing a HTML dynamic service that is common to all categories of Tencent documents. In order to facilitate the generation and deployment of access to various categories, and to follow the trend of cloud migration, I considered using Docker to fix service content and manage product versions in a unified manner. . This article will share the optimization experience I accumulated in the process of serving Docker for your reference.

Let's talk about how to use pkg to package Node.js projects into executable files. Let's talk about how to use pkg to package Node.js projects into executable files. Dec 02, 2022 pm 09:06 PM

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

An in-depth analysis of Node's process management tool 'pm2” An in-depth analysis of Node's process management tool 'pm2” Apr 03, 2023 pm 06:02 PM

This article will share with you Node's process management tool "pm2", and talk about why pm2 is needed, how to install and use pm2, I hope it will be helpful to everyone!

Token-based authentication with Angular and Node Token-based authentication with Angular and Node Sep 01, 2023 pm 02:01 PM

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to

What to do if npm node gyp fails What to do if npm node gyp fails Dec 29, 2022 pm 02:42 PM

npm node gyp fails because "node-gyp.js" does not match the version of "Node.js". The solution is: 1. Clear the node cache through "npm cache clean -f"; 2. Through "npm install -g n" Install the n module; 3. Install the "node v12.21.0" version through the "n v12.21.0" command.

See all articles