How to Prevent refresh, repeated submission, and how to prevent rollback

Source: Internet
Author: User
ArticleDirectory
    • I. Overview
    • Ii. Disable caching
    • Iii. Other Methods
    • Simple operation method: Anti-rollback and refresh
Disable the submit button after submission (Most people do this)

What should I do if I press F5 to refresh after the customer submits it?

Use session

Before submitting the page, that is, before processing the database:

If SESSION ("OK") = true then
Response. Write "error, submitting"
Response. End
End if

After data processing, modify SESSION ("OK") to false.

Data processing is successful and redirect to another page immediately

Refresh after the operation is indeed a problem. You can use to jump to the page and close this page. If there is a parameter data condition to control it, it should be done well. You can directly modify the window. the value of location to change all parameters.

Disadvantage: simply using response. Redirect will no longer work, because we must use the client to convert a user from one page to another.CodeClear location. History. Note that this method clears the last access history instead of all access records. Click the back button, and then click the back button. You can see that the page is opened before this page! (Of course, this is when Javascript is enabled on your client .)

What if the customer presses back? Prevent webpage withdrawal-Disable caching

When we add a database, if the database is allowed to be removed and the page is refreshed, the addition operation will be executed again, which is undoubtedly not what we need, as in general, a lot of code on the Internet that prohibits caching is sometimes not reliable. You only need to add the code to the operation page, specify a new page to be targeted in the page, and then click back, check whether it will not be returned to the previous operation page. In fact, this history has 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 we prevent users from clicking the back button to return to previously browsed pages ?"

Unfortunately, we cannot disable the browser's back button.

Prevent web page bounce-new window

Use window. open pop-up form page. Click Submit to close this page. The ASP page for processing the submitted form is also displayed. set the target of the Form. When you click submit, the window is displayed. open ("XXX. ASP "," _ blank "), and then use js to submit the form. close ();
Simply put, a new window is displayed when the form is submitted and the window is closed. How do I return the window opened by window. open? Where can I go back?

Well, I have a bunch of crap. Do you know how to handle it? Use both client scripts and server scripts.

 

Repeated JSP submission

There are several ways to read the online:
1. Add this code to the head area on your table list 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 session. Add a hidden domain in the form to display
The value of the card. After the form is submitted, a new token is generated, and the token and session submitted by the user are
Token comparison in. If the token is the same, it is submitted repeatedly.
3
Use the response. Redirect ("selfpage") statement in your server-side control code. However, this method is not used in most cases.
There are many other methods...
4
<Input type = "button" value = "Submit" onclick = "This. Disabled = true; this. Form. Submit ()">

5

Add a hidden field to the Form on the JSP page.
<Input type = "hidden" name = "url" value = <% = request. getrequesturl () %>

Add the following statement to your serverlet
String url = request. getparameter ("url ");
Response. sendredirect (URL );
I usually use this method to return the JSP page. I don't quite understand what the concept of refresh is.

6 Ajax refreshing new submission

7. prevent repeated submission of system operations due to browser refresh keys during web development
How can this problem be solved? Redirection can solve the problem of repeated data submission caused by PAGE refresh. We can use redirection to solve this problem. However, in the struts action, mapping. findword (); by default, you can find the page to jump to in the project folder. How can this problem be solved?
Modify the struts-config.xml file, there is a redirect retargeting attribute in action, Struts default is false, add this attribute, change to true, write the absolute or relative address of the page to jump to in forword.
Modify 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>

Difficult browser Problems

The browser's back button makes it easy for us to return to previously accessed pages, which is undoubtedly very useful. But sometimes we have to disable this function to prevent users from disrupting the order of page access. This article describes various methods for disabling the browser's back button on the Internet, and analyzes their advantages and disadvantages and applicable scenarios.

I. Overview

Many people once asked, "How can we 'deactivate 'the browser's back button ?", Or "how can we prevent users from clicking the back button to return to previously browsed pages ?" This is also one of the most frequently asked questions on the ASP forum. Unfortunately, the answer is very simple: we cannot disable the browser's back button.
At first, I felt incredible that someone wanted to disable the browser's back button. Later, I was relieved to see that there were so many people who wanted to disable this button (only the back button, not the browser's forward button ). By default, after submitting a form, you can use the back button to return to the form page (instead of using the "edit" button !), Edit and submit the form again to insert a new record to the database. This is what we don't want to see.
Therefore, I decided to find a way to avoid this situation. I visited many websites and referred to various implementation methods described by these websites. If you frequently access ASP programming websites, you may have seen some of the content introduced in this article. The task in this article is to introduce all possible methods and find the best method!

Ii. Disable caching

Among the many solutions I have found, it is recommended to disable page caching. Specifically, the server script is used as follows:
<%
Response. Buffer = true
Response. expiresabsolute = now ()-1
Response. expires = 0
Response. cachecontrol = "no-Cache"
%>
This method is very effective! It forces the browser to re-access the server download page instead of reading the page from the cache. When using this method, the programmer's main task is to create a session-level variable, using this variable to determine whether the user can still view the page that is not suitable for access through the back button. Because the browser no longer caches this page, when the user clicks the back button, the browser will download the page again. Program You can check the session variable to see if the user is allowed to open the page.

For example, suppose we have the following form:

<%
Response. Buffer = true
Response. expiresabsolute = now ()-1
Response. expires = 0
Response. cachecontrol = "no-Cache"
If Len (Session ("firsttimetopage")> 0 then
& Single; the user has accessed the current page, and now returns access again.
& Single; clear session variables and redirect users to the logon page.
Session ("firsttimetopage") = ""
Response. Redirect "/bar. asp"
Response. End
End if
& Single; if the program runs here, the user can view the current page
& Single; create a form from below
%>
<Form method = post action = "somepage. asp">
<Input type = submit>
</Form>

We use the session variable firsttimetopage to check whether the user has accessed the current page for the first time. If it is not the first time (that is, the session ("firsttimetopage") contains a value), we will clear the value of the session variable and redirect the user to a start page. In this way, when the form is submitted (sompepage. asp is opened at this time), we must assign firsttimetopage a value. That is, we need to add the following code in somepage. asp:
Session ("firsttimetopage") = "no"
In this way, the somepage has been enabled. if ASP users click the back button, the browser will re-request the server to download the page. The server checks that session ("firsttimetopage") contains a value, so the session ("firsttimetopage") is cleared "), and redirect the user to other pages. Of course, all of this requires the user to enable the cookie, otherwise the session variable will be invalid. (For more information about this issue, see for session ariables to work, must the web visitor have cookies enabled ?)
In addition, we can also use the client code to make the browser no longer cache web pages:
<HTML>
<Head>
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "Pragma" content = "no-Cache">
</Head>
If you use the above method to force the browser to stop caching web pages, pay attention to the following points:

"Pragma: No-Cache" prevents the browser from caching pages only when secure connections are used. For unprotected pages, "Pragma: No-Cache" is considered to be the same as "expires:-1". In this case, the browser still caches the page, but marks the page as expired immediately. In IE 4 or 5, the cache-control meta HTTP-EQUIV tag is ignored and does not work.
We can add all the code in practical applications. However, this method is not recommended because it cannot be applied to all browsers. However, in an Intranet environment, the administrator can control which browser the user uses. I think someone will use this method.

Iii. Other Methods

Next we will discuss the method to move the button back itself as the center, rather than the browser cache. Here is an article rewiring the back button for reference. However, I have noticed that if this method is used, even though the user does not see the previous data input page when clicking the back button, it only needs to be clicked twice, which is not the expected effect, many times, stubborn users can always find a way to bypass preventive measures.
Another way to disable the back button is to open a window without a toolbar with client JavaScript, which makes it difficult for users to return to the previous page, but not impossible. A safer but rather annoying method is to open a new window when the form is submitted, while closing the window where the form is located. However, I think this method is not worth considering, because we cannot open a new window every time a user submits a form.
So, can we add JavaScript code to the page we don't want the user to return? The javascript code added to this page can be used to produce the effect of clicking the forward button, which offsets the action generated by clicking the back button. The javascript code used to implement this function is as follows:

As shown in:
<Script language = "JavaScript">
<! --
Javascript: window. History. forward (1 );
// -->
</SCRIPT>

Similarly, although this method is effective, it is still far from the "best method. Later, I saw someone suggested using location. Replace to switch from one page to another. The principle of this method is to replace the current history with the URL of the new page, so that there is only one page in the history browsing, 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 method in any situation. The example using this method is as follows:
<A href = "pagename.htm" onclick = "javascript: location. Replace (this. href );
Event. returnvalue = false; "> do not go back to the link on this page </a>
Do not go back to the link on this page!
The disadvantage of this method is that simply using response. Redirect will no longer work, because every time a user transfers from one page to another, we must use the client code to clear location. History. Note that this method clears the last access history, not all access records.
Click the link above to open a simple HTML page. Click the back button to open the page instead of the page! (Of course, you must enable the client JavaScript code in the browser .)
After careful searching, I found that I still cannot find a way to completely disable the browser's back button. All the methods described here can prohibit users from returning to the previous page in different ways to varying degrees, but they all have their own limitations. Because there is no way to completely disable the back button, the best solution should be to mix client scripts and server scripts.

<HTML>
<Head>
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "Pragma" content = "no-Cache">
</Head>

<Script language = "JavaScript">
<! --
Javascript: window. History. forward (1 );
// -->
</SCRIPT>
</Html>

Asp.net anti-Refresh repeated submission, anti-Rewind method simple operation method anti-refresh and refresh

Add in page_load

Response. cache. setnostore ();

// The variable "issubmit" stored in the session indicates whether the submission is successful.
If (! Ispostback)
If (session ["issubmit"] = NULL)
Session. Add ("issubmit", false );
If (bool) session ["issubmit"])

{

// If the form data is submitted successfully, set "session [" issubmit "]" to false.

Session ["issubmit"] = false;

// Display the submitted information

Textbox1.text = "* submitted successfully! ";

}
Else

{// Otherwise (no submission or page refresh), no information is displayed

Textbox1.text = "";
Response. End ();
}
Add in the submit button
Session ["issubmit"] = true;
Response. Redirect ("this page ");

In addition:

1. Generally, you should judge (uniqueness) at the business layer to solve this problem.

2. Write response. cachecontrol = "no-Cache" to clear the cache for loading events on the page.

3. Some people say this: I have encountered such a problem before. I submitted a resume of a person step by step. After finishing the first page, I jumped to the second page, to prevent the user from returning to the first page and resubmitting the first page, when the user submits the first page for the first time, put the self-increasing ID number of records inserted into the database into the session. When the user returns to the first page from the second page and submits the page again, I will use the value in the session to query the database. If this ID is available, I will use the update statement to write the data on the first page into the database. If this ID is not found, I will use the insert statement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.