本文介绍了HttpServletResponse sendRedirect 永久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这将使用临时 302 HTTP 状态代码重定向请求:
This will redirect a request with a temporary 302 HTTP status code:
HttpServletResponse response;
response.sendRedirect("http://somewhere");
但是是否可以使用永久 301 HTTP 状态代码重定向它?
But is it possible to redirect it with a permanent 301 HTTP status code?
推荐答案
您需要手动设置响应状态和 Location
标头.
You need to set the response status and the Location
header manually.
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", "http://somewhere/");
在 sendRedirect()
之前设置状态将不起作用,因为 sendRedirect()
会在之后将其覆盖为 SC_FOUND
.
Setting the status before sendRedirect()
won't work as sendRedirect()
would overridde it to SC_FOUND
afterwards.
这篇关于HttpServletResponse sendRedirect 永久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!