Until now, we have discussed the HTTP Request, In this article, we will see HTTP Response. We will see what happens when the server responds to the HTTP Request.
HTTP/2.0 404 Not Found Content-Type: text/html Header2: ... ... Header 3: ... HeaderN: ... <html> <head>...</head> <body> ... </body> </html>
Above is the HTTP response. First Line is the status line that shows the version of HTTP for here it is HTTP/2.0 and Error Code 404 and also a short message NOT FOUND.
What is HTTP Response?
HTTP Response is given by the server to the client. The request that the client has generated server processes it analyses it and gives the required response so i.e HTTP response.
What is Server HTTP Response?
As we know when a client requests the server, the server has to respond to the query, so when the server responds to the client along with the response it sends some information in the header. They are as follows:
- Allow: it specifies the request method that the server supports(like GET, POST, etc.)
- Cache-control: it has value public, private, and no-cache.it means the circumstances in which document of response can be cached.
- Connection: This Header has two value
- close: it tells the browser that not to use persistent HTTP
- keepalive: it tells the browser to use persistent HTTP.
- content-Disposition: This Header request to the user to save the file in a disk of the given name.
- Content-Encoding: This header specifies the encoding of the page.
- Content- length: This header specified the number of bytes in the response.
- Content-type: This Header specifies the MIME type of response.
- Expires: This Header specifies the content expiry i.e after how many days it cannot be cached.
- Last Modified: This Header specifies when the document was last modified. The client then caches a document.
- Location: This Header is always included when the status line is 303s.It shows the address of the document.
- Refresh: This Header specifies how soon the browser would ask for an update page. we can specify the time here.
- Retry after: This header is used along with 503s. It tells the client how soon the request can be repeated.
- Set cookie: This Header specifies the cookie with a page.
Methods for HTTP Response Header in Servlet
Let us see the following methods that can be used to give the HTTP Response Header.
- String encodeRedirectURL(String url): This method is used to encode the specified URL in the sendRedirect method. If encoding is not needed, it returns the unchanged URL.
- String encodeURL(String url): It encoded the specified URL by including session Id. If encoding is not needed, it returns the unchanged URL.
- boolean containsHeader(String name): It returns TRUE/FALSE whether the named response is set.
- boolean isCommitted(): Returns TRUE/FALSE for the committed response.
- void addCookie(Cookie cookie): It adds the specified cookie in the response.
- void addDateHeader(String name, long date): Adds a header to the response with the given name and date value.
- void addHeader(String name, String value): It adds the header to response with given name and value.
- void addIntHeader(String name, int value ): It adds the header to response with given name and integer value.
- void flushBuffer(): It forces the content in the buffer to be written in the client.
- void reset(): It clears the data in the buffer.
- void resetBuffer(): It clears the content in the buffer without removing headed or status code.
- void sendError(int sc): It sends the Error response while specifying the status code. also, it clears the buffer.
- void sendError(int sc, String msg): It sends the Error message with the specified status.
- void sendRedirect(String location): It sends the response to the client with the Redirect location URL.
- void setBufferSize(int size): It sets the Buffer size of the body of the response.
- void setCharacterEncoding(String charset): It sends the character Encoding. for eg. to UTF – 8.
- void setContentLength(int length): It sends the length of the content body in the response.
- void setContentType(String type): it sets the content type of the response if the response is not being committed.
- void setDateHeader(String name, long date): It sets the response header with date name and date value.
- void setHeader(String name, String value): It sets the response header with name and value.
- void setIntHeader(String name, int value): It sets a response header with a given name and an integer value.
- void setLocale(Locale loc): It sets the locale of response.
- void setStatus(int sc): It sets the status code for the response.
Let’s see the Example for it:
In this example, we use the setIntHeader() method to the header.