【发布时间】:2016-02-14 21:59:47
【问题描述】:
我想接受来自客户端的数据。 每种方法的优缺点是什么?
HttpServletRequest request = retriveRequest();
Cookie [] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if ("my-cookie-name".equals(cookie.getName())) {
String value = cookie.getValue();
//do something with the cookie's value.
}
}
或
String request.getHeader("header-name");
当我读到How are cookies passed in the HTTP protocol?
Cookie 在请求(客户端 -> 服务器)和响应(服务器 -> 客户端)中作为 HTTP 标头传递。
【问题讨论】:
-
如果没有设置 cookie,request.getCookies() 可能会返回 null,因此请确保在 'for' 循环之前检查 'cookies' 数组不为 null。
-
有什么理由打折请求参数?