servlets response
In the Java Servlet API, the HttpServletResponse
interface represents the response that a servlet sends to the client. It provides methods for setting response headers, writing content to the response body, and sending error codes and redirects.
The HttpServletResponse
interface provides several methods for setting response headers:
setStatus(int sc)
: Sets the status code of the response.setContentType(String type)
: Sets the MIME type of the response.setHeader(String name, String value)
: Sets a response header with the given name and value.addHeader(String name, String value)
: Adds a response header with the given name and value.setIntHeader(String name, int value)
: Sets a response header with the given name and integer value.addIntHeader(String name, int value)
: Adds a response header with the given name and integer value.
The HttpServletResponse
interface also provides several methods for writing content to the response body:
getWriter()
: Returns aPrintWriter
object that can be used to write character data to the response.getOutputStream()
: Returns aServletOutputStream
object that can be used to write binary data to the response.setContentLength(int len)
: Sets the length of the response content in bytes.setBufferSize(int size)
: Sets the buffer size used for the response.flushBuffer()
: Forces any content in the response buffer to be written to the client.
Finally, the HttpServletResponse
interface provides methods for sending error codes and redirects:
sendError(int sc)
: Sends an error response with the given status code.sendError(int sc, String msg)
: Sends an error response with the given status code and message.sendRedirect(String location)
: Sends a redirect response to the given location.
By using the HttpServletResponse
interface, servlets can generate dynamic content and control the response sent to the client. This can be useful for tasks such as generating HTML pages, handling form submissions, and providing API responses.