为什么 HttpServletRequest 中没有 getCookie(String) 方法?
作为一名软件顾问,我从人类诞生之日起就一直在使用 Servlet API,但我一直想知道,为什么 HttpServletRequest 中没有 getCookie(String) 方法?
API 仅定义了 getCookies() 方法,该方法返回 Cookie 数组,因此,如果您要在 Servlet 级别查找某些特定 cookie,则必须迭代整个数组,拥有 getCookie 方法会很棒,不是吗?思考?
As a software consultant, I have been using the Servlet API since the dawn of the man, but I always want to know, why there isn't a getCookie(String) method in the HttpServletRequest?
The API only defines the getCookies() method wich returns an array of Cookies, so if you are looking for some specific cookie at the Servlet level you have to iterate over the whole array, having a getCookie method would be great, don't you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以肯定的是,这里没有人可以为您的问题提供最终答案,但您应该记住 Servlet API 是一个相当低级的东西。它提供了围绕 HTTP 协议的基本抽象,这就是为什么全世界都使用提供更高程度抽象的库(spring、struts、jsf、lift 等)。
顺便说一句:cookie 名称不一定是唯一的,cookie 通过其名称和路径来标识。
Pretty sure, no one here can provide a final answer to your question, but you should keep in mind that the Servlet API is a fairly low-level thing. It provides basic abstractions around the HTTP protocol, that's why the whole world uses libraries providing a higher degree of abstraction (spring, struts, jsf, lift, etc.).
BTW: The cookie name must not necessarily be unique, a cookie is identified by its name and path.