1

I have a servlet named EditPhotos which, believe it or not, is used for editing the photos associated with a certain item on a web design I am developing. The URL path to edit a photo is [[SITEROOT]]/EditPhotos/[[ITEMNAME]].

When you go to this path (GET), the page loads fine. You can then click on a 'delete' link that POSTs to the same page, telling it to delete the photo. The servlet receives this delete command properly and successfully deletes the photo. It then sends a redirect back to the first page (GET).

For some reason, this redirect fails. I don't know how or why, but using the HTTPFox plugin for firefox, I see that the POST request receives 0 bytes in response and has the code NS_BINDING_ABORTED.

The code I am using to send the redirect, is the same code I have used throughout the website to send redirects:

response.sendRedirect(Constants.SITE_ROOT + "EditPhotos/" + itemURL);

I have checked the final URL that the redirect sends, and it is definitely correct, but the browser never receives the redirect. Why?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121

2 Answers2

1

Read the server logs. Do you see IllegalStateException: response already committed with the sendRedirect() call in the trace?

If so, then that means that the redirect failed because the response headers are already been sent. Ensure that you aren't touching the HttpServletResponse at all before calling the sendRedirect(). A redirect namely exist of basically a Location response header with the new URL as value.

If not, then you're probably handling the request using JavaScript which in turn failed to handle the new location.

If neither is the case or you still cannot figure it, then we'd be interested in the smallest possible copy'n'pasteable code snippet which reproduces exactly this problem. Update then your question to include it.


Update as per the comments, the culprit is indeed in JavaScript. A redirect on a XMLHttpRequest POST isn't going to work. Are you using homegrown XMLHttpRequest functions or a library around it like as jQuery? If jQuery, please read this question carefully. It boils down to that you need to return a specific response and then let JS/jQuery do the new window.location itself.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Could not find IllegalStateException or any Exception at all in the logs. The POST itself is sent via JavaScript, could this be the problem? I'll try and find a short snippet of the problem. – DanielGibbs Feb 02 '11 at 01:04
  • Oops, forgot to refresh the page for a while. I managed to fix it by changing the JavaScript, I have posted it below. – DanielGibbs Feb 02 '11 at 02:19
0

Turns out that it was the JavaScript I was using to send the POST that was the problem. I originally had this:

<a href="#" onClick="javascript:deletePhoto(781)">Delete</a>

And everything got fixed when I changed it to this:

<a href="#" onClick="javascript:deletePhoto(781); return false;">Delete</a>

The deletePhoto function is:

function deletePhoto(photoID) {
    doPost(document.URL, {'action':'delete', 'id':photoID});
}

function doPost(path, params) {
    var form = document.createElement("form");
    form.setAttribute("method", "POST");
    form.setAttribute("action", path);
    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);
        form.appendChild(hiddenField);
    }
    document.body.appendChild(form);
    form.submit();
}
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • Not meant to be harsh, but those kind of JavaScripts always give me itch. Why not just a form with a button? You can just use CSS to style the button to look like a plain link if necessary. – BalusC Feb 02 '11 at 02:27
  • To be honest, i'm not sure why exactly, but I think I found it neater at the time to do it this way. Having lots of little forms and trying to make them styled and layed out properly would give me CSSeadaches. – DanielGibbs Feb 02 '11 at 02:34
  • Websites are supposed to function equally with JS disabled (mobiles!). JS is supposed to only do progressive enhancements. CSS is pretty trivial once you understand it. – BalusC Feb 02 '11 at 02:42