Servlet API provides two important interfaces ServletResponse and HttpServletResponse to assist in sending response to client.
Servlet API提供了两个重要的接口ServletResponse和HttpServletResponse来帮助将响应发送到客户端。
ServletResponse的一些重要方法 (Some Important Methods of ServletResponse)
Methods | Description |
---|---|
PrintWriter getWriter() | returns a PrintWriter object that can send character text to the client. |
void setBufferSize(int size) | Sets the preferred buffer size for the body of the response |
void setContentLength(int len) | Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header |
void setContentType(String type) | sets the content type of the response being sent to the client before sending the respond. |
void setBufferSize(int size) | sets the preferred buffer size for the body of the response. |
boolean isCommitted() | returns a boolean indicating if the response has been committed |
void setLocale(Locale loc) | sets the locale of the response, if the response has not been committed yet. |
方法 | 描述 |
---|---|
PrintWriter getWriter() | 返回一个PrintWriter对象,该对象可以将字符文本发送到客户端。 |
void setBufferSize(int size) | 设置响应主体的首选缓冲区大小 |
void setContentLength(int len) | 设置响应中内容主体的长度在HTTP servlet中,此方法设置HTTP Content-Length标头 |
void setContentType(String type) | 设置发送响应之前发送给客户端的响应的内容类型。 |
void setBufferSize(int size) | 设置响应主体的首选缓冲区大小。 |
boolean isCommitted() | 返回一个布尔值,指示响应是否已提交 |
setLocale(Locale loc) 无效 | 设置响应的语言环境(如果尚未提交响应)。 |
HttpServletResponse接口 (HttpServletResponse Interface)
HttpServletResponse interface adds the methods that relates to the HTTP response.
HttpServletResponse接口添加了与HTTP响应相关的方法。
HttpServletResponse的一些重要方法 (Some Important Methods of HttpServletResponse)
Methods | Description |
---|---|
void addCookie(Cookie cookie) | adds the specified cookie to the response. |
void sendRedirect(String location) | Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer |
int getStatus() | gets the current status code of this response |
String getHeader(String name) | gets the value of the response header with the given name. |
void setHeader(String name, String value) | sets a response header with the given name and value |
void setStatus(int sc) | sets the status code for this response |
void sendError(int sc, String msg) | sends an error response to the client using the specified status and clears the buffer |
方法 | 描述 |
---|---|
无效addCookie(Cookie cookie) | 将指定的cookie添加到响应中。 |
void sendRedirect(String location) | 使用指定的重定向位置URL向客户端发送临时重定向响应并清除缓冲区 |
int getStatus() | 获取此响应的当前状态代码 |
字符串getHeader(String name) | 获取具有给定名称的响应头的值。 |
void setHeader(String name, String value) | 设置具有给定名称和值的响应头 |
无效setStatus(int sc) | 设置此响应的状态码 |
void sendError(int sc, String msg) | 使用指定的状态向客户端发送错误响应并清除缓冲区 |
翻译自: https://www.studytonight.com/servlet/servlet-response.php