You are on page 1of 14

HTTP Response

• The response, response is a process to


responding against it request.
• Response Object in JSP is used to send
information, or output from web server to
the user.
• Response Object sends output in form of
stream to the browser.
• This can be redirecting one file to another
file .
• Response object tells browser what output
has to use or display on browser, and
what stream of data contain PDF,
html/text, Word, Excel.
• The result or the information of a request
is denoted with this object.
• The response object handles the output of
the client.
• This contrasts with the request object.
• The class or the interface name of the
response object is
http.HttpServletResponse.
• The response object is written:
Javax.servlet.http.httpservletresponse.
• The response object is generally used by
cookies.
• The response object is also used with
HTTP Headers.
HTTP Response Object Work in
Servlet

Http Client
HttpServlet

doGet() {
Process request from Client
Request using Request object;

Send response to client via


Response the Response object;
}

Session
Methods
• setContentType();
• addCookie(Cookie cookie)
• addHeader(String name, String value)
• containsHeader(String name)
• setHeader(String name, String value)
• sendRedirect(String)
• sendError(int status_code)
setContentType()

• setContentType() method of response object is used to


set the MIME type and character encoding for the page.

• General syntax of setContentType() of response object


is as follows:

response.setContentType();
• For example:

response.setContentType("text/html");

• The above statement is used to set the


content type as text/html dynamically.
addCookie(Cookie cookie)

• addCookie() method of response object is


used to add the specified cookie to the
response.
• The addcookie() method is used to write a
cookie to the response.
• If the user wants to add more than one
cookie, then using this method by calling it
as many times as the user wants will add
cookies.
• General syntax of addCookie() of response
object is as follows:

response.addCookie(Cookie cookie)

For example:

response.addCookie(Cookie exforsys);

The above statement adds the specified


cookie exforsys to the response.
addHeader(String name, String value)

• Adds response header with given name and


value
• If the header is already present, then value is
added to the existing header values.
• General syntax of addHeader() of response
object is as follows:
response.addHeader(String name, String
value)

• Here the value of string is given as second


parameter and this gets assigned to the header
given in first parameter as string name.
• For example:

response.addHeader("Author", Exforsys");

• The output of above statement is as below:

Author: Exforsys
sendRedirect(String)

• Sends a redirect response to client with redirect


specified URL location. Contain Relative path
• Thus the sendRedirect method of the response
object enables one to forward a request to a new
target.
• But one must note that if the JSP executing has
already sent page content to the client, then the
sendRedirect() method of response object will
not work and will fail.
• General syntax of sendRedirect of
response object is as follows:

response.sendRedirect(String) ;

• In the above the URL is given as string.

For example:

response.sendRedirect("http://abc.test.co
m/error.html");

You might also like