java 使用 sendRedirect URL 传递标头参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17227511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:22:46  来源:igfitidea点击:

Pass header parameters with sendRedirect URL

javaurlhttp-headers

提问by user2507575

How to pass header parameters with sendRedirectURL?

如何通过sendRedirectURL传递标头参数?

I am trying to do sendRedirectto different application. But I want to pass header parameters along with request. How to do this?

我正在尝试sendRedirect对不同的应用程序进行操作。但是我想将标头参数与请求一起传递。这个怎么做?

回答by c.P.u1

HttpServletResponse.sendRedirect()sends a redirect response(302) to the client. The client then initiates a new request to the resource specified in the Locationheader. HTTP doesn't define a way for a server to ask the client to set any request headers to a redirected resource.

HttpServletResponse.sendRedirect()向客户端发送重定向响应(302)。然后,客户端向Location标头中指定的资源发起新请求。HTTP 没有定义服务器要求客户端将任何请求标头设置为重定向资源的方式。

To pass data to the new resource, you could set it in the query string:

要将数据传递给新资源,您可以在查询字符串中设置它:

To make it concrete

为了使它具体

response.sendRedirect("NewResource?param1=value1");

NewResource

新资源

request.getParameter("param1");

回答by Ryan Stewart

If I'm understanding you correctly, it's not possible. It sounds like you want something like this:

如果我理解正确,那是不可能的。听起来你想要这样的东西:

Client -> ServerA: 
GET /foo

ServerA -> Client: 
301 Moved Permanently
<Something magic to force the client to send header "Header1: xxx" to ServerB>

Client -> ServerB:
GET /bar
Header1: xxx

That "something magic" doesn't exist. The client will always send whatever header values it wants to send. There's not even a standard way with a redirect to suggestthat a client do something like that.

那个“神奇的东西”不存在。客户端将始终发送它想要发送的任何标头值。甚至没有标准的重定向方式来建议客户做类似的事情。

回答by Suresh Atta

The following lines sets the http headersto the response:

以下几行将http 标头设置为response

    response.setHeader(header1, value1);
    response.setHeader(header2, value2);
     -----
     -----
     response.sendredirect("someJsp.jsp")