HttpServletRequest的属性字段如何映射到原始HTTP请求? [英] How the attribute field of a HttpServletRequest maps to a raw HTTP request?

查看:119
本文介绍了HttpServletRequest的属性字段如何映射到原始HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

HttpServletRequest属性数据存储在原始HTTP请求中的位置?它是否在请求的主体中?

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

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

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:

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

我应该在哪里放'my.username '原始HTTP请求中的数据,以便'myAttribute'字符串在归属后收到值John Doe?

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?

推荐答案

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

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.

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

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.

这篇关于HttpServletRequest的属性字段如何映射到原始HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆