12.1 JSP Response Overview
As we discussed that client sends a header and a data to server , similarly server in turn sends response header and requested data.
If you look at the source code of a web page in your browser, you will only see the HTML portion and not the HTTP headers, even though they actually have been transmitted together.
Sample structure of JSP response looks like
HTTP/1.1 200 OK Content-Type : text/html Header2 : ... ... HeaderN : ... (Blank Line) <!DOCTYPE ...> <HTML> <HEAD> ... </HEAD> <BODY> ... </BODY> </HTML>
As a part of response header , server sends several information like method that a server supports , in which page has been encoded while transmission, length of the content etc.
Specification provides us a API in HttpServletResponse which allows us to get both type of information.
The response object is an instance of a javax.servlet.http.HttpServletResponse. Each time a client requests a page the JSP engine creates a new instance of response object
12.2 HTTP Response Header
Following is the list of most commonly used Response Headers
· Allow –This header specifies the methods ( like Get , POST )supported by server.
· Content-Encoding- The header specifies the type of encoding used on the data.
· Content-Language- This header specifies the language (like en_US, en_GB ) in which content is written
· Content-Length- This header specifies the length of the response body in bytes.
· Content-Disposition – This header instruct the browser to raise a "File Download" dialogue box for a known MIME type with binary format or suggest a filename for dynamic content.
· Refresh- This header specifies how soon the browser should ask for an updated page. You can specify time in number of seconds after which a page would be refreshed.
· Date- This header specifies the date and time when the message was sent
· Expires- This header specifies the date and time after which the response is considered stale.
· Last-Modified - This header specifies the date when the requested resource was last modified date.
· Location-This header is used for redirection and is included when the response status is 300s. The browser automatically reconnects to this location and retrieves the new document.
· Set-Cookie- The Set-Cookie header specifies a cookie associated with the page. Each cookie requires a separate Set-Cookie header
· Retry-After- This header tells the client how soon it can repeat its request
This is the Http protocol based request and extends ServletResponse Interface
As HttpServletResponse extends Servlet Response all of its methods will be available. . Below are the commonly used methods –
· PrintWriter getWriter() - returns a PrintWriter object that can send the character text to the client.
· void setContentType(String type) - sets the content type of the response being sent to the client before sending the respond.
· void addCookie(Cookie cookie)- this methods adds the cookie object to the response.
· void sendRedirect(String url) this method redirects the request to a specified URL. In this entire process browser initiate a new request. Since this will be a new request, any objects in request will not be available.
· boolean containsHeader(String name)- this method is used to verify if header with given name is has already been set in response header.
· String encodeURL(String url)- this method encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.
· String encodeRedirectURL(String url)- this method encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.
· void setHeader(String headerName, String headerValue)- This method sets the response header with the designated name to the given value.
· void setDateHeader(String header, long milliseconds)- This is the convenience method to set the date related headers.
· void setIntHeader(String header, int headerValue)- This is the convenience method to set the integer related headers and we need not to convert any String value to integer explicitly.
· void sendError(int code)- this method sends an error response to the client using the specified status code.
· void sendError(int code, String message)- this method sends an error message along with the status code to the client.
Note: HTTP allows multiple occurrences of the same header name, and you sometimes want to add a new header rather than replace any existing header with the same name
· void addHeader(String headerName, String headerValue)- This method adds the response header with the given name to the given value as compared to setHeader which updates the existing value.
· void addDateHeader(String header, long milliseconds)- This is the convenience method to add the date related headers as compared to addDateHeader which updates the existing value.
· void addIntHeader(String header, int headerValue)- This is the convenience method to add the integer related headers and we need not to convert any String value to integer explicitly .
12.4 JSP Response Examples
Question :How to redirect user to “google.com”?
Create one redirect.jsp inside with below content
<html> <head> <title> Redirect Example </title> </head> <body> <% response.sendRedirect("http://www.google.com"); %> </body> </html>
Testing – Access redirect.jsp using http://localhost:8080/jsp-tutorial/redirect.jsp . You will see Google will be opened.
Question : Write an example to use sendError method using any dummy error code and message ?
Create one sendError.jsp inside with below content
<html> <head> <title> Send Error Example </title> </head> <body> <% response.sendError(567,”This is error message ”); %> </body> </html>
Testing – Access redirect.jsp using http://localhost:8080/jsp-tutorial/sendError.jsp . You will see below figure stating both error code and message.
Question : Write an example to explain Refresh response header using setHeader APIs?
Create one refresh.jsp inside in which we will set ‘Refresh’ header using setIntHeader API. Header name will be ‘Refresh’ and we will set value as 5 so after every 5 seconds , page will refresh.
<html> <head> <title> Refresh Example </title> </head> <body> <H4> This is Refresh Page Example .Page will auto refresh after every 10 seconds </H4> <br/> <% response.setIntHeader("Refresh", 5); out.println(new java.util.Date()); %> </body> </html>
Testing – Access redirect.jsp using http://localhost:8080/jsp-tutorial/refresh.jsp . You will see below figure displaying time and date. After every 5 seconds, this page will be refreshed (you can verify it by time displayed )
12.5 HTTP Status Codes
As mentioned in introduction section, server responds status code in first line of response. There are several status codes and each status code has a specific meaning
· status code 200 means request is OK .
· status code 404 means requested resource is not available.
· status code 400 means bad request
· status code 403 means access is forbidden.
· status code 503 means service is unavailable.
In sendError.jsp we have used sendError method with dummy error code. You can use any one of the predefined status code as well