This will redirect a request with a temporary 302 HTTP status code:
HttpServletResponse response; response.sendRedirect("http://somewhere");
But is it possible to redirect it with a permanent 301 HTTP status code?
sendRedirect() method redirects the response to another resource, inside or outside the server. It makes the client/browser to create a new request to get to the resource. It sends a temporary redirect response to the client using the specified redirect location URL.
The main important difference between the forward() and sendRedirect() method is that in case of forward(), redirect happens at server end and not visible to client, but in case of sendRedirect(), redirection happens at client end and it's visible to client.
If you forward the request, you can use setAttribute to put your variable in the request scope, which can easily be retrieved in your JSP. request. setAttribute( "temp" , yourVar); RequestDispatcher dispatcher = request.
You need to set the response status and the Location
header manually.
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader("Location", "http://somewhere/");
Setting the status before sendRedirect()
won't work as sendRedirect()
would overridde it to SC_FOUND
afterwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With