How to redirect with one time message after PUT


So here is the desired behavior I'm trying to get:

  1. User goes to a password change web page.
  2. User fills out form and the form PUTs to our REST server.
  3. After a successfully PUT, the user is redirected to the "My Account" page with a message saying, "Password successfully changed".
  4. If they refresh their "My Account" page, the password message should go away. (I.e. it is a one time message.)

Here are solutions I've tried (unsuccessfully):

1) Using JQuery to perform an AJAX PUT to the REST server. This works fine for the PUT, but the redirect has to be in the onSuccess JavaScript and if it passes a message on the URL to the My Account page, then that message hangs around after refresh.

2) Using a traditional form. However, this won't do a PUT ( method does not support put, on post and get). I could do a POST, but that is "wrong" from a REST perspective, because I'm updating the user account record, not creating a new record. The redirect and one-time message could all be handled server side with this solution (RESTlets, Servlets and/or JSP).

Is there are good solution out there? Or must I change my PUT to a POST?

By : Stephen M


You may do PUT using a traditional form using JavaScript. The only trick is to flash a temporary message. I've seen web frameworks that use a sort of temporary session state for this kind of stuff which requires little effort on your part. If you don't have that available, the trick would be to store a temporary session in the database and reference it through a cookie. It is not straight forward, and that is why web frameworks can really help you in this situation.

The typical setup is as simple as this:

$('form').submit(function () {
    url: 'service/api',
    data: $('form').serialize(),
    type: 'POST'})
  .done(function () {
      // perform redirect

On the server side you set the temporary session state and delete when complete.

This video can help you solving your question :)
By: admin