JSP Server Response

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.

  1. What is JSP Server Response?
  2. 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)
  3. The HttpServletResponse class and its Method
  4. 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
  1. Allow: It specifies the request methods that the server supports.
  2. Cache-Control: It specifies the circumstances in which the response document can safely be cached.
  3. Connection: It instructs the browser whether to use persistent HTTP connections or not.
  4. 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.
  5. Content-Encoding: It specifies the way in which the page was encoded during transmission.
  6. Content-Language: It signifies the language in which the document is written.
  7. Content-Length: It indicates the number of bytes in the response.
  8. Content-Type: It gives the MIME type of the response document.
  9. Expires: It specifies the time at which the content should be considered out of date.
  10. Last-Modified: It indicates when the document was last changed.
  11. Location: It includes all the responses that have a status code in 300s.
  12. Refresh: It specifies how soon the browser should ask for an updated page.
  13. Retry-After: It is used with a response to tell the client how soon it can repeat its request.
  14. Set-Cookie: It specifies a cookie associated with the page.
HttpServletResponse Object

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:
  1. encodeRedirectURL(String url): It is used to encode the specified URL for use in the sendRedirect method.
  2. containsHeader(String name): It returns True or False indicating whether the named response header has already been set.
  3. isCommitted(): It returns True or False indicating if the response has been committed.
  4. addCookie(): It is used to add the specified cookie to the response.
  5. addHeader(String name, String value): It is used to add a response header with the given name and value.
  6. flushBuffer(): It is used to force any content in the buffer to be written to the client.
  7. reset(): It is used to clear the data that exists in the buffer as well as the status code and headers.
  8. sendError(int sc): It is used to send an error response to the client using the specified status code and clearing the buffer.
  9. sendRedirect(String location): It is used to send a temporary redirect response to the client using the specified redirect location URL.
  10. setBufferSize(int size): It is used to set the preferred buffer size for the body of the response.
  11. setContentType(int len): It is used to set the length of the content body on the response.
  12. setContentType(String type): It is used to set the content type of the response being sent to the client.
  13. setHeader(String name, String value): It is used to set a response header with the given name and value.
  14. setLocale(Locale loc): It is used to set the locale of the response.
  15. 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.

index.html
<!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>
welcome.jsp
<%@ 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>
Output

Run your code to get the following output:

HTTP Server Response Header Example in JSP

Type your name and click on the “GO” Button, you will be redirected to “Google.com”.

JSP Server Response Headers with Examples

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.

Leave a Reply

Your email address will not be published.