gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:03:35 26 4
gpt4 key购买 nike

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

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

HttpServletRequest 属性数据存储在原始 HTTP 请求中的什么位置?它在请求的正文中吗?

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

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

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

最佳答案

需要说明的是,我认为@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");

关于java - HttpServletRequest 的属性字段如何映射到原始 HTTP 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/911529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com