In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to deal with the problem of repeated submission, repeated refresh and preventing fallback in web. The article is very detailed and has certain reference value. Interested friends must finish reading it!
one. Preface
You will see such a problem in any of the more professional BBS, even if you Google, you will find that there are a lot of people paying attention and asking, but the solutions given by everyone are very different, (some people advocate using scripts to solve it; some want to redirect to other pages; some want to raise the problem to Token's point of view) Why is there such a big difference?
two. Problem scenario
First of all, we should understand why we have to deal with such a problem. Or professionally, what is the right scene for it? (it seems that only people come to ask and no one to explain)
one. Repeatedly submitted, repeatedly refreshed scene
Repeated submission and repeated refresh are all to solve the problem of repeated records of the system. In other words, someone is submitting a record multiple times (why? Maybe there is nothing to do when you are idle; it is most likely that the user does not know whether his submission result has been executed at all. ).
However, such a problem does not necessarily have to be dealt with, depending on the type of system you are developing. For example, if you take over a resource management system, the system itself does not allow "duplicate" records at all from the perspective of requirements. Under the constraints of such requirements, performing repeated submission actions will only lead to "business-level exceptions". It is impossible to execute successfully and there is no need to avoid unavoidable problems.
two. A scene that prevents a retreat.
Now that we know the scenario of repeated refresh and repeated commit, let's take a look at the reason for the "prevent fallback" operation. For example, if you are developing a voting system, it has a lot of steps, and there are connections between these steps, such as the first step will send some information to the second step, and the second step will cache the information. At the same time, send your own information to the third step. Wait, wait, What mistake? The most typical is that such an operation directly leads to the loss of information for the first step! (if such information is stored on Request, of course you can store it in Session or a larger context, but this is not a good idea! With regard to the issue of information storage, we will discuss it in detail next time.
three. How to deal with the problem
Of course, many systems (for example, the booking system itself allows individuals to book tickets repeatedly) must avoid repeated refreshes, repeated submissions, and backwards, but even such problems, also need to distinguish between how to deal with it and where to deal with it (the Internet just tells you how to handle it, but rarely distinguishes where) Obviously, there are no more than two ways to deal with it on the client side or the server side, and the ways to deal with different locations are also different, but one thing to declare in advance: any client (especially the BWeiS side) processing is not trusted, the best and most should be the server-side processing method.
Client processing:
Facing the client, we can use Javascript script to solve the problem, as follows
one. Repeat refresh, repeat submission
Ways One: set a variable that allows only one submission.
The code is as follows:
Var checkSubmitFlg = false
Function checkSubmit () {
If (checkSubmitFlg = = true) {
Return false
}
CheckSubmitFlg = true
Return true
}
_ document.ondblclick = function docondblclick () {
Window.event.returnValue = false
}
_ document.onclick = function doc {
If (checkSubmitFlg) {
Window.event.returnValue = false
}
}
five
Add a hidden field to the FORM form on the JSP page
Add the following statement to your serverlet
String url=request. GetParameter_r ("url")
Response.sendRedirect (url)
I usually use this method to return JSP pages, so I don't quite understand what you mean by repeated refresh.
6 ajax No refresh submission
7. Prevent the repeated submission of system operations caused by the refresh keys of the browser in Web development.
How to solve it? Redirection can solve the problem of repeated submission of data caused by page refresh, and we can naturally use redirection to solve this problem. But mapping.findword () in struts's action; if you jump, the default is to find the page you want to jump to in the project folder. How to solve this kind of situation?
Modify the struts-config.xml file. There is an attribute of redirect redirection in action. The default in struts is false. Add this attribute, change it to true, and write the absolute or relative address of the page to be redirected in forword.
The modifications are as follows:
Difficult problems related to browsers
The browser's back button allows us to easily return to previously visited pages, which is undoubtedly very useful. But sometimes we have to turn this feature off to prevent users from disrupting the predetermined order of page visits. This paper introduces various schemes that can be found on the network to disable the browser back button, and analyzes their respective advantages and disadvantages and applicable situations.
I. Overview
Many people have asked, "how can I 'disable' the browser's back button?" , or "how can I prevent users from clicking the back button to return to previously viewed pages?" This question is also one of the most frequently asked questions on the ASP forum. Unfortunately, the answer is simple: we can't disable the browser's back button.
At first I was surprised that someone would want to disable the browser's back button. Later, I was relieved to see that so many people wanted to disable this back button (only the back button, not the browser's forward button). Because by default, users can use the back button to return to the form page after submitting the form (instead of using the Edit button! And then edit and submit the table again to insert a new record into the one-way database This is what we do not want to see.
So I decided to find a way to avoid this. I visited many websites and referred to the various implementation methods introduced by these sites. If you visit the ASP programming website frequently, you may have seen some of the contents of this article. The task of this article is to introduce all possible methods to you, and then find out the best one!
II. Prohibit caching
Among the many scenarios I have found, there is a suggestion to disable page caching. Specifically, use a server-side script, as follows:
This method is very effective! It forces the browser to revisit the server download page instead of reading the page from the cache. With this approach, the programmer's main task is to create a session-level variable that determines whether the user can still view a page that is not suitable for access through the back button. Because the browser no longer caches the page, the browser will download the page again when the user clicks the back button, and the program can check the session variable to see if the user should be allowed to open the page.
For example, suppose we have the following form:
0 then
& the single; user has visited the current page and is now returning to visit it again.
& single; clears the session variable and redirects the user to the login page.
Session ("FirstTimeToPage") = ""
Response.Redirect "/ Bar.asp"
Response.End
End If
& single; if the program runs here, it means that the user can view the current page
Start creating the form below & single;
% >
We use the session variable FirstTimeToPage to check whether the user is visiting the current page for the first time. If this is not the first time (that is, Session ("FirstTimeToPage") contains a value), then we clear the value of the session variable and redirect the user to a start page. So, when the form is submitted (when SompePage.asp is opened), we must give FirstTimeToPage a value. That is, in SomePage.asp, we need to add the following code:
Session ("FirstTimeToPage") = "NO"
In this way, if a user who has opened SomePage.asp clicks the back button, the browser will rerequest the server to download the page, and the server checks that Session ("FirstTimeToPage") contains a value, so it clears Session ("FirstTimeToPage") and redirects the user to another page. Of course, all of this requires the user to enable Cookie, otherwise the session variable will be invalid. (for more information on this issue, see For session ariables to work, must the Web visitor have cookies enabled?)
Alternatively, we can use client-side code to stop browsers from caching Web pages:
If you use the above method to force the browser to no longer cache Web pages, you must pay attention to the following points:
"Pragma: no-cache" prevents browsers from caching pages only when using secure connections. For unsecured pages, "Pragma: no-cache" is considered the same as "Expires:-1", where the browser still caches the page but marks it as immediately expired. In IE 4 or 5, the "Cache-Control" META HTTP-EQUIV flag is ignored and has no effect.
We can add all this code in practical application. However, because this method is not suitable for all browsers, it is not recommended. But if the administrator can control which browser the user uses in the Intranet environment, I think some people will still use this method.
III. Other methods
The method we're going to discuss next is that the back button itself is centered rather than the browser cache. Here is an article Rewiring the Back Button that is worth referring to. However, I have noticed that with this method, although the user will not see the page where he previously entered the data when he clicks the back button, it only needs to be clicked twice, which is not what we want, because very often, stubborn users can always find ways to bypass preventive measures.
Another way to disable the back button is to use the client JavaScript to open a window without a toolbar, which makes it difficult, but not impossible, for the user to return to the previous page. A safer but rather annoying approach is to open a new window when the form is submitted while closing the window in which the form is located. But I don't think this approach deserves serious consideration, because we can't let users open a new window every time they submit a form.
So, can we also add JavaScript code to the page that we don't want users to return? The JavaScript code added to this page can be used to produce the effect of clicking the forward button, which counteracts the action caused by the user clicking the back button. The JavaScript code used to implement this function is as follows
As shown:
Similarly, although this method is effective, it is still a long way from the "best way". Then I saw that someone suggested using location.replace to move from one page to another. The principle of this approach is to replace the current history with the URL of the new page so that there is only one page in the browsing history and the back button will never become available. I think this may be the method that many people are looking for, but it is still not the best way in any case. An example of using this method is as follows:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.