Temporary Redirect(307):临时重定向。在HTTP1.1的规范:10.3.8 307 Temporary Redirect定义如下:
The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.
The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s) , since many pre-HTTP/1.1 user agents do not understand the 307 status. Therefore, the note SHOULD contain the information necessary for a user to repeat the original request on the new URI.
If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
废话很多,总的来说有意义的感觉就是:
下面是用TOMCAT+SERVLET做的一个DEMO。
节约篇幅,无package和import,请自己Ctrl+Shift+O
/** * HTTP 协议,Temporary Redirect(307) 测试 * * @author zhangpu * */ public class Http307TestServlet extends HttpServlet { private static final long serialVersionUID = 7047368191379656914L; static final String tempRedirectUrl = "http://www.baidu.com"; @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(307); resp.setHeader("Location", tempRedirectUrl); PrintWriter p = resp.getWriter(); p.println("Location:" + tempRedirectUrl); p.println("Note: this is 307 redirect Url."); p.close(); } }
web.xml中加入Servlet的定义:
<servlet> <servlet-name>temporary redirect test</servlet-name> <servlet-class>org.acooly.http.Http307TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>temporary redirect test</servlet-name> <url-pattern>/http/307</url-pattern> </servlet-mapping>
启动TOMCAT,浏览器中输入:http://localhost:8080/http/307
OW,直接开始百度。