JSP - Response

As you know, in dynamic web application development, client and server interactions are necessary to send and receive information over the Internet. When the browser requests a webpage, a lot of information is sent to the webserver, and the webserver responds after processing the HTTP request. Such a response is part of an HTTP header response and cannot be read directly. In this chapter, you will learn about the various methods provided by JSP for managing JSP response.

JSP Response

The JSP response can be defined as an implicit object is an instance of "HttpServletResponse" and is formed for each JSP request created by the JSP container.

The response Implicit Object

  • A response object is an implicit object implemented to modify or deal with the reply sent to the client (i.e., browser) after processing the request, such as redirect responding to another resource or an error sent to a client.
  • The response implicit object is an instance of a javax.servlet.http.HttpServletResponse interface.
  • The container creates it for every request.

Here is an example of a JSP request and response implicit objects where a user submits login information, and another JSP page receives it for processing:

Example (HTML file):
<!DOCTYPE html>
<html>
    <head>
        <title>User login form</title>
    </head>
    <body>
        <form action="login.jsp">
            Please enter Username: <input type="text" name="u_name" /> <br />
            <input type="submit" value="Submit Details" />
        </form>
    </body>
</html>
Example (login.jsp):
<%@ page import = " java.util.* " %>
<% 
String username = request.getParameter("u_name"); 
if(username.equals("admin")){
    response.sendRedirect("home.jsp");  
}else{
    out.print("Invalid Username");  
}
%>

Methods of response Implicit Object

JSP has different methods for dealing with implicit response objects, which is of type HttpServeltResponse. Each of these methods has its own functionality.

  • String encodeURL(String url): is used for encoding a particular URL by comprising the session ID along with it. Otherwise, when the encoding is not required, it will return the URL unchanged.
  • String encodeRedirectURL(String url): is used for encoding a particular URL for using it in the sendRedirect method. Otherwise, when the encoding is not required, it will return the URL unchanged.
  • boolean containsHeader(String name): is used for returning a Boolean signifying whether the name response header is already set or not.
  • void addDateHeader(String name, long date): is used for adding a response header with the given name along with a date-value.
  • void addHeader(String name, String value): is used to add a response header with the given name and a value.
  • boolean isCommitted(): is used for returning a Boolean value signifying whether the response has been committed or not.
  • void addIntHeader(String name, int value): is used for adding a response header with the specified name along with an integer value.
  • void flushBuffer(): is used for forcing any content in the buffer for writing it to the client.
  • void addCookie(Cookie cookie): is used for adding a particular cookie with the response.
  • void reset(): is used for clearing any data which is present in the buffer, header or status code.
  • void sendError(int sc): is used for sending an error response (through particular status code) to its client.
  • void sendError(int sc, String msg): is used for sending an error response (through some particular status string) to its client.
  • void resetBuffer(): is used for clearing the content of the underlying buffer without clearing the status code or header.
  • void sendRedirect(String location): is used for sending a short-term redirect response to the client.
  • void setBufferSize(int size): is used for setting an ideal buffer size for the response body.
  • void setCharacterEncoding(String charset): is used for setting the character encoding (usually MIME charset - UTF 8) being sent to the client.
  • void setContentType(String type): is used for setting the content type being sent to the client when the response has not yet been committed.
  • void setDateHeader(String name, long date): is used to set a response header with the given name and date-value.
  • void setContentLength(int len): is used for setting the length of the body of content in the response.
  • void setHeader(String name, String value): is used for setting a response header with the given name as well as value.
  • void setIntHeader(String name, int value): is used to set a response header with the given name and an integer value.
  • void setLocale(Locale loc): is used for setting the locale of the response in case the response is not yet committed.
  • void setStatus(int sc): is used for setting the status code for your response.

Scroll Back to Top