In the previous article, we have discussed what are the pieces of information sent by the client that is stored in the request header of the HTTP request i.e Client Request in JSP.
In this article, we will discuss Server Response in JSP. 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 the 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 JSP
Let’s 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 the given name and value.
- void addIntHeader(String name, int value ): It adds the header to response with the given name and an 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 HTTP Response in JSP
In this example, we will use the setIntHeader() method from the response object to set the interval for page refresh.
This method will send back header “Refresh” to the browser along with an integer indicating time interval in seconds (5 sec). Let’s see the code of page refresh in.
<%@page import="java.util.GregorianCalendar"%> <%@page import="java.util.Calendar"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% response.setIntHeader("Refresh",5); Calendar calendar=new GregorianCalendar(); String am_pm; int hr=calendar.get(Calendar.HOUR); int min=calendar.get(Calendar.MINUTE); int sec=calendar.get(Calendar.SECOND); if(calendar.get(Calendar.AM_PM)==0) am_pm="AM"; else am_pm="PM"; String ct=hr+":"+min+":"+sec+" "+am_pm; out.print("Current Time "+ct); %> </body> </html>
Thus we have learned what happens when the server responds to HTTP Request and how the request is manipulated.
In the next article of this tutorial, we will cover How Form is processed in JSP with a simple example.