How to avoid resubmit in jsp when refresh?
Asked Answered
L

7

22

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 ?

Latoria answered 23/8, 2009 at 1:17 Comment(0)
A
23

Here's an explanation of the problem...

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.

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.

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.

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.

Afterheat answered 23/8, 2009 at 2:26 Comment(2)
First of all, thanks very much! We write a MVC framework ourself, when submit this form it will use request.getRequestDispatcher(jspPath + "/" + nextPage).forward(request,response); to direct to next page, this will cause resubmit form problem, I am still consider a solution to solve it, any recommendations will be appreciated.Latoria
Instead of the forward() method, you need to use response.sendRedirect(jspPath + "/" + nextPage).Afterheat
G
15

The idiom you want here is either:

  1. POST+REDIRECT+GET; or
  2. AJAX form submission.

POST+REDIRECT+GET works like this:

  1. Form submissions use the POST method;
  2. When the JSP or (hopefully) servlet receives the POST, it does whatever it needs to do (saves or updates data or whatever);
  3. The servlet or JSP then uses a Location: HTTP header to redirect the user to what's probably that same URL.

The benefit of this is that clicking reload won't resubmit the form. Also if you click the browser back button you won't get prompted with the "submit again?" dialog box.

Here is a JSP example of this.

The AJAX submit means that instead of submitting the form back to the server in the traditional sense you create an AJAX request back to the server with the form data. That submit does what it needs to. Clicking reload will simply reload the page. It won't resend the AJAX request.

Greff answered 23/8, 2009 at 1:46 Comment(3)
1.POST+REDIRECT+GET, is very good, but I need to submit form in the same page many times, if i always redirect to another page, it is trouble, any other better solution ? and by the way, i want to know, why this resubmit of forms happened?Latoria
It shouldn't be much trouble. The page returned by the GET can include the same form; it can even be the same JSP page; the form can even be populated with the data which the user just entered. The difference is that refreshing the page will not be interpretted by the web server as submitting the form.Afterheat
We write a MVC framework ourself, when submit this form it will use request.getRequestDispatcher(jspPath + "/" + nextPage).forward(request,response); to direct to next page, this will cause resubmit form problem, I am still consider a solution to solve it, any recommendations will be appreciated.Latoria
V
5

It works using the following script.

<script>
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>
Valdis answered 26/6, 2019 at 7:12 Comment(1)
dirty but beautiful, tyCornu
M
0

Don't use GET for submitting or otherwise changing data. Use POST instead. You'll want to change your form to read

<form action="test.jsp" method="get">

See http://www.google.com/search?q=get+post for more info.

(Your question is also a bit unclear - do you mean refreshing in browser, or in the container (eg Tomcat)? What do you mean resubmit the page? Pages don't get submitted, forms do. Which system? I guessed what you meant, if you meant something else, let us know.)

Methodize answered 23/8, 2009 at 1:32 Comment(1)
yeah, your guess is absolute right, since every time i refresh the page, the form will be submitted, and result in duplicate records in databaseLatoria
S
0

IF you are using JSP in the serverside and using a controller Servlet to control the page direction. You can simply change Page direction mode from Forward to Page Redirection. This way you can prevent Form resubmit. this would be userful to understand it

regards!

Susian answered 25/8, 2010 at 9:41 Comment(0)
A
0

Here is the solution

@RequestMapping(value="masterrulescreation")
protected ModelAndView masterrules(HttpServletRequest request, HttpServletResponse     response) throws Exception {
    try {   

        if(request.getParameter("ruleCode")!=null && request.getParameter("ruleCode")!="") //check the primary key is not null ,if goes for submission
        {
        request.setCharacterEncoding("UTF-8");

        String xxx=request.getParameter("xxx");
        S
        boolean result=object.method(xxx);
        String message="";//reurning a messgae is sucess
        if(result==true)
        {
            message="Data Saved successfully";
        }
        else
        {
            message="Error while saving data";  
        }

        return  (new ModelAndView(getMasterrulescreation(),"message",message));
        }
        else //if no value in primary key redirect fresh page 
        {
            return  (new ModelAndView(getMasterrulescreation()));
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}   
Altonaltona answered 19/8, 2014 at 9:46 Comment(0)
C
0

We can use Post/Redirect/Get (PRG) pattern to solve the issue of multiple submission of same data.

It works as follows:

First time when a user submits a form to server by POST or GET method, then we update the state in application database.

Then we send a redirect response to send reply to client.

Then we load a view by using GET command. There is no data is sent in this. Since this a new JSP page, it is safe from multiple submits. The code that processes the request is idempotent. So it does not do same action twice for same request.

enter image description here

Carlisle answered 9/6, 2018 at 1:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.