Back to: JSP Tutorials for Beginners and Professionals
JSP Server Response Headers
In this article, I am going to discuss JSP Server Response Headers with Examples. Please read our previous article where we discussed JSP Client Request. At the end of this article, you will understand the following pointers.
- What is JSP Server Response?
- Understanding Response Headers (Allow, Cache-Control, Connection, Content-Disposition, Content-Encoding, Content-Language, Content-Length, Content-Type, Expires, Last-Modified, Location, Refresh, Retry-After, Set-Cookie)
- The HttpServletResponse class and its Method
- HTTP Server Response Header Example
What is JSP Server Response?
When a client request is processed, the response is generated from the webserver which consists of a status line, response headers, a blank line, and a document. We can access the webserver HTTP response to the browser by using JSP. JSP Server Response is a response object of HTTPServletResponse class which is an instance of javax.servlet.http.HttpServletResponse object.
JSP Server Response Headers
- Allow: It specifies the request methods that the server supports.
- Cache-Control: It specifies the circumstances in which the response document can safely be cached.
- Connection: It instructs the browser whether to use persistent HTTP connections or not.
- Content-Disposition: It lets you request that the browser ask the user to save the response to disk in a file of the given name.
- Content-Encoding: It specifies the way in which the page was encoded during transmission.
- Content-Language: It signifies the language in which the document is written.
- Content-Length: It indicates the number of bytes in the response.
- Content-Type: It gives the MIME type of the response document.
- Expires: It specifies the time at which the content should be considered out of date.
- Last-Modified: It indicates when the document was last changed.
- Location: It includes all the responses that have a status code in 300s.
- Refresh: It specifies how soon the browser should ask for an updated page.
- Retry-After: It is used with a response to tell the client how soon it can repeat its request.
- Set-Cookie: It specifies a cookie associated with the page.
It is an instance of javax.servlet.http.HttpServletResponse object which provides HTTP-specific functionality in sending a response. The servlet container creates and passes it as an argument to the servlet’s service method.
Methods of HttpServletResponse object:
- encodeRedirectURL(String url): It is used to encode the specified URL for use in the sendRedirect method.
- containsHeader(String name): It returns True or False indicating whether the named response header has already been set.
- isCommitted(): It returns True or False indicating if the response has been committed.
- addCookie(): It is used to add the specified cookie to the response.
- addHeader(String name, String value): It is used to add a response header with the given name and value.
- flushBuffer(): It is used to force any content in the buffer to be written to the client.
- reset(): It is used to clear the data that exists in the buffer as well as the status code and headers.
- sendError(int sc): It is used to send an error response to the client using the specified status code and clearing the buffer.
- sendRedirect(String location): It is used to send a temporary redirect response to the client using the specified redirect location URL.
- setBufferSize(int size): It is used to set the preferred buffer size for the body of the response.
- setContentType(int len): It is used to set the length of the content body on the response.
- setContentType(String type): It is used to set the content type of the response being sent to the client.
- setHeader(String name, String value): It is used to set a response header with the given name and value.
- setLocale(Locale loc): It is used to set the locale of the response.
- setStatus(int sc): It is used to set the status code for the response.
HTTP Server Response Header Example
In this example, we are redirecting the response to “Google”. Here we are receiving the input from the user on the index.html page and redirecting it on the welcome.jsp page using response implicit object.
<!DOCTYPE html> <html> <head> <title>implicit object</title> </head> <body bgcolor="pink"> <form action="welcome.jsp"> <input type="text" name="uname"> <input type="submit" value="GO"><br /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body bgcolor="pink"> <% response.sendRedirect("http://www.google.com"); %> </body> </html>
Run your code to get the following output:
Type your name and click on the “GO” Button, you will be redirected to “Google.com”.
In the next article, I am going to discuss HTTP Status Codes in JSP with Examples. Here, in this article, I try to explain JSP Server Response Headers with Examples. I hope you enjoy this JSP Server Response article.