Java HttpServletRequest 的属性字段如何映射到原始 HTTP 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/911529/
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-08-11 20:59:41  来源:igfitidea点击:

How the attribute field of a HttpServletRequest maps to a raw HTTP request?

javahttpservlets

提问by Alceu Costa

In Java, the attribute field of a HttpServletRequest object can be retrieved using the getAttribute method:

在 Java 中,可以使用 getAttribute 方法检索 HttpServletRequest 对象的属性字段:

String myAttribute = request.getAttribute("[parameter name]");

Where the HttpServletRequest attribute data is stored in a raw HTTP request? Is it in the body of the request?

HttpServletRequest 属性数据存储在原始 HTTP 请求中的何处?它在请求的正文中吗?

For example, I'm trying to create a raw GET HTTP request that will be sent to my servlet using some client program. My servlet.doGet() method would be something like this:

例如,我正在尝试创建一个原始的 GET HTTP 请求,该请求将使用某些客户端程序发送到我的 servlet。我的 servlet.doGet() 方法是这样的:

public void doGet(HttpServletRequest request, HttpServletResponse response)
{
     String myAttribute = request.getAttribute("my.username");
     ...
}

Where should I put the 'my.username' data in the raw HTTP request so that the 'myAttribute' String receives the value "John Doe" after the attribution?

我应该将“my.username”数据放在原始 HTTP 请求中的什么位置,以便“myAttribute”字符串在归属后接收值“John Doe”?

采纳答案by Eddie

To add to @gid's answer, attributes are not present in any way in the HTTP request as it travels over the wire. They are created (by your code) when processing the request. A very common use is to have a server set (aka create) some attributes and then forward to a JSP that will make use of those attributes. That is, an HTTP request arrives and is sent to a Servlet. The Servlet attaches some attributes. Additional server-side processing is done, eventually sending the page to a JSP, where the attributes are used. The response is generated in the JSP. The HTTP request and the HTTP response do not contain any attributes. Attributes are 100% purely server-side information.

要添加到@gid 的答案中,HTTP 请求通过网络传输时不以任何方式存在属性。它们是在处理请求时(由您的代码)创建的。一个非常常见的用途是让服务器设置(也称为创建)一些属性,然后转发到将使用这些属性的 JSP。也就是说,一个 HTTP 请求到达并被发送到一个 Servlet。Servlet 附加了一些属性。完成额外的服务器端处理,最终将页面发送到使用属性的 JSP。响应在 JSP 中生成。HTTP 请求和 HTTP 响应不包含任何属性。属性是 100% 纯粹的服务器端信息。

When a single given HTTP request has completed, the attributes become available for garbage collection (unless they are persisted in some other location, such as a session). Attributes are only associated with a single request object.

当单个给定的 HTTP 请求完成时,这些属性可用于垃圾收集(除非它们保留在某个其他位置,例如会话)。属性仅与单个请求对象相关联。

回答by Gareth Davis

Just to be clear as I think @Jon's answer doesn't make it perfectly clear. The values for getAttribute and setAttribute on HttpServletRequest are not present on what is actually sent over the wire, they are server side only.

只是要清楚,因为我认为@Jon 的回答并没有完全清楚。HttpServletRequest 上的 getAttribute 和 setAttribute 值不存在于实际通过线路发送的内容中,它们仅用于服务器端。

// only visible in this request and on the server
request.getAttribute("myAttribute"); 

// value of the User-Agent header sent by the client
request.getHeader("User-Agent"); 

// value of param1 either from the query string or form post body
request.getParameter("param1"); 

回答by Eddie

I think what he is really asking is "how do I get parameteres into my program", not attributes. If that is the question, then send parameters in the GET request as part of the URL (after a question mark, http://myhost.com/myapp?name=joe&age=26) then retrieve them using request.getParameter("name") and request.getParameter("age"), or whatever you need.

我认为他真正要问的是“如何将参数添加到我的程序中”,而不是属性。如果这是问题,那么在 GET 请求中发送参数作为 URL 的一部分(在问号之后,http://myhost.com/myapp?name=joe&age=26)然后使用 request.getParameter("name ") 和 request.getParameter("age"),或者任何你需要的。