Servlet redirect makes the browser do the work
A redirect lets the servlet off the hook completely. After deciding that it can’t do the work, the servlet simply calls the sendRedirect() method:

Using relative URLs in sendRedirect()
You can use a relative URL as the argument to sendRedirect(), instead of specifying the whole “http://www...” thing. Relative URLs come in two flavors: with or without a starting forward slash (“/”).
Imagine the client originally typed in:

But if the argument to sendRedirect() DOES start with a forward slash:

The Container builds the complete URL relative to the web Container itself, instead of relative to the original URL of the request. So the new URL will be:

Watch it!
You can’t do a sendRedirect() after writing to the response!
That’s probably obvious, but it’s the LAW so we’re just making sure.
If you look up sendRedirect() in the API, you’ll see that it throws an IllegalStateException if you try to invoke it after “the response has already been committed.”
By “committed”, they mean that the response has been sent. That just means the data has been flushed to the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access