HttpClient请求设置属性问题

2 java parameters attributes httpclient

我使用这个 HttpClient 库玩了一段时间(几周)。我想以某种方式将属性设置为请求。不是参数而是属性。在我的 servlet 中,我想使用 Integer inte = (Integer)request.getAttribute("obj");

我不知道我想念什么。我试试这个。

NameValuePair[] pair = new NameValuePair[1];
pair[0] = new NameValuePair();
pair[0].setName("aloha");
pair[0].setValue("value");
Run Code Online (Sandbox Code Playgroud)

但这个设置参数不是属性。我也使用这个,因为这只是一个具有接受字符串和对象的方法的对象。还是没有解决。

HttpClientParams clParam = new HttpClientParams();
clParam.setParameter("obj", new Integer(24405));
method.setParams(clParam);
Run Code Online (Sandbox Code Playgroud)

请给我一些线索......谢谢。

jsi*_*ght 5

我相信您误解了setAttribute/getAttribute方法的目的。放入“getAttribute”检索请求中的数据只能通过服务器上的 setAttribute 调用进行设置。客户端无法强制在那里设置值,因为将参数从客户端传递到服务器的唯一方法是通过参数(或 POST 请求中的某种其他结构)。

getAttribute/setAttribute 实际上用于在使用RequestDispatcher时在服务器代码片段之间传递信息。