ovod
ovod

Reputation: 1178

java jsp web page caches POST request

I have JSP page with user registration form and add submit button:

<input type="submit" name="action" value="add"/>

Form method is POST. My problem here is that when I fill form, push add - everything works - user is added, but when I press refresh page - the same POST request is sent, PSQL exception is thrown (dublicate users) and my application stops working (because I configured so). My question is how to disable such actitvity - meaning when I press refresh, I get empty page.

Upvotes: 0

Views: 71

Answers (1)

Jan
Jan

Reputation: 13858

As @kryger stated - Post/Redirect/Get is the pattern for you.

The main idea is to redirect the browser after the POST to load next page with GET. That way, if the browser is refreshed / page is reloaded, this reload will happen on the GET - which is safe to do.

In JSP redirecting can be a bit tricky - if you don't have any Servlets or else in place you need to make sure that

response.sendRedirect("pagetoget.jsp"); 
return; 

is called before response is committed (before any bytes are written). If you have JSP and JSP alone, you should do that by starting your jsp with <% and do that code right in the beginning.

Upvotes: 1

Related Questions