刷新时如何避免在jsp中重新提交? [英] How to avoid resubmit in jsp when refresh?

查看:156
本文介绍了刷新时如何避免在jsp中重新提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写程序,但是遇到问题: 当我刷新jsp页面时,系统会自动重新提交整个页面, 而且我不知道如何避免 有人可以帮我吗?

I am writing a program, but i encounter a problem: when I refresh the jsp page, system will automatically resubmit the whole page, and i don't know how to avoid it, can someone help me ?

推荐答案

以下是对该问题的解释...

Here's an explanation of the problem...

单击表单上的提交"按钮会将请求发送到Web服务器,其中包括在表单上输入的所有数据.不仅URL,而且表单数据都是请求的一部分,浏览器会记住该请求.如果用户单击刷新",浏览器将重复该请求,并将相同的URL 表单数据再次发送到Web服务器.

Clicking the "submit" button on a form sends a request to the web server, which includes all the data entered on the form. Not only the URL but also the form data is part of the request, and this request is remembered by the browser. If the user clicks "refresh", the browser repeats the request, sending the same URL and form data to the web server again.

但是表单可以通过两种不同的方式提交,即GET或POST,具体取决于"form"标签的"method"属性.约定GET请求没有副作用.它仅获取数据,而不对数据库进行任何更改.另一方面,如果请求更改了数据,则应始终使用POST请求.就像我说的那样,这些只是约定,它们之间并没有太多技术上的区别,但是一个非常重要的区别是,浏览器在尝试重复POST时会警告用户-单击刷新"将弹出一个对话框.警告用户这可能会导致重复操作,并确认他们确实要重新提交.刷新GET请求时,浏览器不会显示此确认.

But forms can be submitted in two different ways, GET or POST, depending on the "method" attribute of the "form" tag. There is a convention that a GET request has no side-effects; it only fetches data but does not make any changes to the database. On the other hand, if a request changes data it should always use a POST request. As I said, these are only conventions, and there is not much technical difference between them, but a very important difference is that browsers will warn the user if they try to repeat a POST -- clicking "refresh" will pop up a dialog box warning the user that this may cause an operation to be repeated, and confirming that they really want to resubmit. The browser does not show this confirmation when refreshing a GET request.

@mk怀疑您的表单使用的是GET方法吗?如果是这样,将其更改为POST是最简单的解决方案,因为这至少意味着如果用户尝试刷新,则会警告用户.

Is your form using the GET method, as suspected by @mk? If so, changing it to POST is the simplest solution, since this will at least mean that the user is warned if they try to refresh.

但是更好的解决方案是@cletus建议的POST + REDIRECT + GET惯用语.这将数据库更新(POST)和视图(GET)分为两个操作.在浏览器上单击刷新,然后仅重复GET,而没有副作用.

But a better solution is the POST+REDIRECT+GET idiom suggested by @cletus. This splits the database update (POST) and the view (GET) into two operations. Clicking refresh on the browser then merely repeats the GET, which has no side-effects.

这篇关于刷新时如何避免在jsp中重新提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆