Servlet Tutorials

Introduction to Servlet Response
Previous Home Next
adplus-dvertising

In Servlet API makes two important interfaces ServletResponse and HttpServletResponse to assist in sending response to client.

There are most important Methods of ServletResponse

MethodDescription
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.

HttpServletResponse Interface

HttpServletResponse interface adds the methods that relates to the HTTP response.

Some Important Methods of HttpServletResponse

MethodDescription
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
Previous Home Next