Home > php教程 > PHP开发 > 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

高洛峰
Release: 2016-12-06 13:52:39
Original
1254 people have browsed it

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 "错误,正在提交"
  response.end
end if
Copy after login

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"
Copy after login

ASP.NET:

Response.Buffer=true;
Response.ExpiresAbsolute=DateTime.Now.AddSeconds(-1);
Response.Expires=0;
Response.CacheControl="no-cache";
Copy after login

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">
Copy after login

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()">
Copy after login

5 Add a hidden field in the FORM form of the JSP page

<input  type="hidden"  name="url"value=<%=request.getRequestURL()%>>
Copy after login

Add the following statement in your serverlet (Java code)

   
String  url=request.getParameter("url"); 
response.sendRedirect(url);
Copy after login

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>
Copy after login

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

曾经有许多人问起,“怎样才能‘禁用'浏览器的后退按钮?”,或者“怎样才能防止用户点击后退按钮返回以前浏览过的页面?”在ASP论坛上,这个问题也是问得最多的问题之一。遗憾的是,答案非常简单:我们无法禁用浏览器的后退按钮。

起先我对于居然有人想要禁用浏览器的后退按钮感到不可思议。后来,看到竟然有那么多的人想要禁用这个后退按钮,我也就释然(想要禁用的只有后退按钮,不包括浏览器的前进按钮)。因为在默认情况下,用户提交表单之后可以通过后退按钮返回表单页面(而不是使用“编辑”按钮!),然后再次编辑并提交表单向数据库插入新的记录。这是我们不愿看到的。

因此我就决定要找出避免出现这种情况的方法。我访问了许多网站,参考了这些网站所介绍的各种实现方法。如果你经常访问ASP编程网站,本文所介绍的部分内容你可能已经见到过。本文的任务是把各种可能的方法都介绍给大家,然后找出最好的方法!

二、禁止缓存

在我找到的许多方案中,其中有一种建议禁止页面缓存。具体是使用服务器端脚本,如下所示:

<%  
Response.Buffer  =  True  
Response.ExpiresAbsolute  =  Now()  -  1  
Response.Expires  =  0  
Response.CacheControl  =  "no-cache"  
%>
Copy after login

这种方法非常有效!它强制浏览器重新访问服务器下载页面,而不是从缓存读取页面。使用这种方法时,编程者的主要任务是创建一个会话级的变量,通过这个变量确定用户是否仍旧可以查看那个不适合通过后退按钮访问的页面。由于浏览器不再缓存这个页面,当用户点击后退按钮时浏览器将重新下载该页面,此时程序就可以检查那个会话变量,看看是否应该允许用户打开这个页面。

例如,假设我们有如下表单:

<%  
Response.Buffer  =  True  
Response.ExpiresAbsolute  =  Now()  -  1  
Response.Expires  =  0  
Response.CacheControl  =  "no-cache"  
If  Len(Session("FirstTimeToPage"))  >  0  then  
&single;  用户已经访问过当前页面,现在是再次返回访问。  
&single;  清除会话变量,将用户重定向到登录页面。  
Session("FirstTimeToPage")  =  ""  
Response.Redirect  "/Bar.asp"  
Response.End  
End  If  
&single;  如果程序运行到这里,说明用户能够查看当前页面  
&single;  以下开始创建表单  
%>  
<form  method=post  action="SomePage.asp">  
<input  type=submit>  
</form>
Copy after login

我们借助会话变量FirstTimeToPage检查用户是否是第一次访问当前页面。如果不是第一次(即Session("FirstTimeToPage")包含某个值),那么我们就清除会话变量的值,然后把用户重新定向到一个开始页面。这样,当表单提交时(此时SompePage.asp被打开),我们必须赋予FirstTimeToPage一个值。即,在SomePage.asp中我们需要加上下面的代码:
Session("FirstTimeToPage") = "NO"

这样,已经打开SomePage.asp的用户如果点击后退按钮,浏览器将重新请求服务器下载页面,服务器检查到Session("FirstTimeToPage")包含了一个值,于是就清除Session("FirstTimeToPage"),并把用户重定向到其他页面。当然,所有这一切都需要用户启用了Cookie,否则会话变量将是无效的。

另外,我们也可以用客户端代码使浏览器不再缓存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>
Copy after login

如果使用上面的方法强制浏览器不再缓存Web页面,必须注意以下几点:

只有在使用安全连接时“Pragma: no-cache”才防止浏览器缓存页面。对于不受安全保护的页面,“Pragma: no-cache”被视为与“Expires: -1”相同,此时浏览器仍旧缓存页面,但把页面标记为立即过期。在IE 4或5中,“Cache-Control”META HTTP-EQUIV标记将被忽略,不起作用。

在实际应用中我们可以加上所有这些代码。然而,由于这种方法不能适用于所有的浏览器,所以是不推荐使用的。但如果是在Intranet环境下,管理员可以控制用户使用哪种浏览器,我想还是有人会使用这种方法。

三、其他方法

接下来我们要讨论的方法以后退按钮本身为中心,而不是浏览器缓存。这儿有一篇文章Rewiring the Back Button很值得参考。不过我注意到,如果使用这种方法,虽然用户点击一下后退按钮时他不会看到以前输入数据的页面,但只要点击两次就可以,这可不是我们希望的效果,因为很多时候,固执的用户总是能够找到绕过预防措施的办法。

另外一种禁用后退按钮的办法是用客户端JavaScript打开一个没有工具条的窗口,这使得用户很难返回前一页面,但不是不可能。一种更安全但相当恼人的方法是,当表单提交时打开一个新的窗口,与此同时关闭表单所在的窗口。但我觉得这种方法不值得认真考虑,因为我们总不能让用户每提交一个表单就打开一个新窗口。

那么,在那个我们不想让用户返回的页面是否也可以加入JavaScript代码呢?在这个页面中加入的JavaScript代码可用来产生点击前进按钮的效果,这样也就抵消了用户点击后退按钮所产生的动作。用于实现该功能的JavaScript代码如下所示:

<script  language="JavaScript">  
<!--  
javascript:window.history.forward(1);  
//-->  
</script>
Copy after login

同样地,这种方法虽然有效,但距离“最好的方法”还差得很远。后来我又看到有人建议用location.replace从一个页面转到另一个页面。这种方法的原理是,用新页面的URL替换当前的历史纪录,这样浏览历史记录中就只有一个页面,后退按钮永远不会变为可用。我想这可能正是许多人所寻求的方法,但这种方法仍旧不是任何情况下的最好方法。使用这种方法的实例如下所示:

<A  HREF="PageName.htm"  onclick="javascript:location.replace(this.href);  
  event.returnValue=false;">禁止后退到本页面的链接</A>
Copy after login

禁止后退到本页面的链接!

这种方法的缺点在于:简单地运用Response.Redirect将不再有效,这是因为每次用户从一个页面转到另一个页面,我们都必须用客户端代码清除location.history。另外还要注意,这种方法清除的是最后一个访问历史记录,而不是全部的访问记录。

点击上面的链接,你将打开一个简单的HTML页面。再点击后退按钮,你可以看到这时打开的不是本页面,而是本页面之前的页面!(当然,你必须在浏览器中启用了客户端JavaScript代码。)

经过一番仔细的寻寻觅觅之后,我发现仍旧无法找出真正能够完全禁用浏览器后退按钮的办法。所有这里介绍的方法都能够在不同程度上、以不同的方式禁止用户返回前一页面,但它们都有各自的局限。由于不存在能够完全禁用后退按钮的方法,所以最好的方案应该是:混合运用客户端脚本和服务器端脚本。

<html>  
<head>  
<meta  http-equiv="Expires"  CONTENT="0">  
<meta  http-equiv="Cache-Control"  CONTENT="no-cache">  
<meta  http-equiv="Pragma"  CONTENT="no-cache">  
</head>   
 

Copy after login

Asp.net中防刷新重复提交、防后退方法

简单操作方法防后退和刷新

Page_Load中加入

Response.Cache.SetNoStore();
 
//Session中存储的变量“IsSubmit”是标记是否提交成功的
if (!IsPostBack)
if (Session["IsSubmit"]==null)
Session.Add("IsSubmit",false);
if ((bool)Session["IsSubmit"])
 
{
 
//如果表单数据提交成功,就设“Session["IsSubmit"]”为false
 
Session["IsSubmit"] = false;
 
//显示提交成功信息
 
TextBox1.Text = " * 提交成功!";
 
}
else
 
{//否则的话(没有提交,或者是页面刷新),不显示任何信息
 
TextBox1.Text = "";
Response.End();
}
Copy after login

提交按钮中加入

Session["IsSubmit"] = true;
Response.Redirect ("本页");
Copy after login

   

另外:

1、通常应该在业务层进行判断(唯一性)解决这种问题

2、要在页面装载事件写上    Response.CacheControl = "no-cache"     清除缓存

3、也有人这样说:我以前也碰到过这样的问题,是在分步提交中一个人的简历,在写完第一个页面后跳到第二个页面,为了防止用户用后退返回到第一个页面,再重新提交第一个页面,我是当用户提交第一次提交第一个页面时,把插入数据库中的记录的自增长id号放到session里,当用户从第二个页面返回到第一个页面再一次提交该页面时,我就用session里的值去数据库查,如果有这个id就用update语句把第一个页面的数据写进数据库,如果没有查到这个id,就用insert语句。


Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template