本文介绍了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?

推荐答案

您需要设置响应状态和位置标题手动。

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永久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 08:03