response.sendRedirect 与 out.println("REDIRECT=SOMEURL")

response.sendRedirect vs out.println("REDIRECT=SOMEURL")

有什么区别
response.sendRedirect("http://www.someurl.com");

PrintWriter out = response.getWriter();
out.println("REDIRECT=http://www.someurl.com");

我知道 sendRedirect() 是如何工作的,但是 out.println("REDIRECT=http://www.someurl.com"); 在这里做什么?

I'd been working on a Payment Gateway service in which the response is sent to my servlet. And in my servlet I perform some operation and using response.sendRedirect() redirecting to response page, which is not working. While out.println() is working.

那只是针对该服务的。他们显然需要 HTTP 200 响应,其中 plain/text 内容的格式与特定服务所期望的格式完全相同,而不是真正的 HTTP 3xx response with a Location header。这样他们就会反过来关心重定向而不是你自己。基本上,客户端从您的服务器转到该支付服务,然后转到给定的重定向 URL,而不是从您的服务器转到给定的重定向 URL.

这完全取决于客户的期望。真实客户端(Web 浏览器)至少只期望带有 Location header 的 HTTP 3xx 响应才能执行重定向。

以下

response.sendRedirect("http://example.com");

在幕后的效果与

相同
response.setStatus(302);
response.setHeader("Location", "http://example.com");