File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Combine Redirect and Forward Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Combine Redirect and Forward" Watch "Combine Redirect and Forward" New topic

Combine Redirect and Forward

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Is there an HTTP header or other mechanism to force the browser to change the URL shown in the address bar (not redirect to a new URL to be loaded)? For example, you click a link with the URL "http://server/servlet?id=10&op=load", the servlet runs, and when it returns it tells the browser to display "http://server/servlet" as the URL for the page?

It's like client-side redirect since it changes the URL, but it's like server-side redirect (forward) in that it doesn't tell the browser to actually load another URL.

Here's the background:

I have a fairly simple servlet that displays a status screen with operation links when no parameters received. The operation links hit the same servlet, this time with parameters, that tell it what to do. I kept it all as one servlet since it's pretty simple.

Now, when an operation is performed, the work is done and the updated status screen is displayed. However, I want the URL in the browser to be the no-parameter status screen URL so they don't inadvertantly reload the page and perform the operation again. I used a client-side redirect to do this, and it works great.

However, now I'd like to display an error message on the status screen if the operation fails. Since it redirects to the status screen, the message must be stored in the session or elsewhere to save it. I can do this, but it seems far more work than is needed.

Does anyone know a simple solution like I outlined at the start? Thanks!
Frederic Filiatrault
Ranch Hand

Joined: Jul 12, 2004
Posts: 69

Sorry to ask but, did you tried to use POST instead of GET to remove your parameters and then, empty your doGet function in your servlet to make sure it will not be reprocess again?

Quite simple but had to ask...

F.<br />SCJP, SCJD, SCWCD, SCBCD<br /> <br />Failure is not an option... It comes with the software bundle.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
No, I haven't tried that. I'm more a backend guy, so forgive my ignorance on this. I assume that to use POST with plain links (anchor tags) I'd have to use JavaScript to set some hidden form fields and submit the form; is that correct?

Upon thinking about it a little more, it seems that using the HTTP session might work pretty well.

In fact, I suspect that I can't do as I originally hoped as it's probably a security risk to allow a web server to set the URL for the loaded page since the user will think that the browser actually loaded the page at the new URL, yet it didn't.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
using HTTP forward calls is completely transparent to the client, he never knows it happens.
There's no security risk as you can only forward to a relative URI (so one within the same domain). If your customers consider that a security risk, fire them and find other customers

I agree. Here's the link:
subject: Combine Redirect and Forward
It's not a secret anymore!