Head First Servlets&JSP 读书笔记_4
HttpServletRequest接口扩展了ServletRequest接口,发生了什么?
与上同理的还有HttpServletResponse接口扩展了ServletResponse接口。以ServletRequest接口为例解释如下:
avax.servlet Interface ServletRequest
Defines an object to provide client request information to a servlet. The servlet container creates a ServletRequest object and passes it as an argument to the servlet’s service method.
[扩展译]当Servlet容器接收到客户端要求访问特定Servlet的请求时,容器先解析客户端的原始请求数据,把它包装成一个ServletRequest对象。当容器调用Servlet对象的service()方法时,就可以把ServletRequest对象呢作为参数传给service()方法。
A ServletRequest object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend ServletRequest can provide additional protocol-specific data (for example, HTTP data is provided by HttpServletRequest.
[译]ServletRequest对象提供参数名、参数值、属性、输入流等数据。其扩展接口(比如HttpServletRequest会提供Http数据)会格外增加特定的协议数据。
Http协议数据主要有如下:
| method | description |
|---|---|
| getContextPath():String | Returns the portion of the request URI that indicates the context of the request.[译]获得Request中传入的URI部分(注:URI标记了资源,未给出资源地址,故只是URL的一部分,参考:Web基础-Uri跟Url的区别) |
| getCookies():Cookie[] | Returns an array containing all of the Cookie objects the client sent with this request.[译]获得Request相关的cookie数组 |
| getHeader(String name):String | Returns the value of the specified request header as a String.[译]获得客户的平台和浏览信息 |
| getSession():HttpSession | Returns the current session associated with this request, or if the request does not have a session, creates one.[译]返回客户相关的Session会话 |
| getInputStream():ServletInputStream | Retrieves the body of the request as binary data using a ServletInputStream[译]返回请求体中的二进制内容(请求体若要处理计算机驱动,可能包含二进制)[注]此为ServletRequest的方法 |
| … | … |
| 其他参数参考:Servlet技术浅析(三)之—–ServletRequest接口和HttpServletRequest接口 |
