Thursday, March 3, 2011

Response Object

learn java
The response object is an instance of a class that implements the javax.servlet.http.HttpServletResponseT
interface.
The response object denotes the HTTP Response data.
The result or the information of a request is denoted with this object.
The response object handles the output of the client.
The response object is generally used by cookies.

The response object is also used with HTTP Headers.

Methods of response Object:
There are numerous methods available for response object. Some of them are:

    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.
response.setContentType("text/html");

addCookie(Cookie cookie):

addCookie() method of response object is used to add the specified cookie to the response.
For example:
response.addCookie(Cookie exforsys);

addHeader(String name, String value):
addHeader() method of response object is used to write the header as a pair of name and value to the response.
If the header is already present, then value is added to the existing header values.
For example:
response.addHeader("Author", "Exforsys");

The output of above statement is as below:
Author: Exforsys

containsHeader(String name):
containsHeader() method of response object is used to check whether the response already includes the header given as parameter.
If the named response header is set then it returns a true value.response.containsHeader(String name)

setHeader(String name, String value):
setHeader method of response object is used to create an HTTP Header with the name and value given as string.
If the header is already present, then the original value is replaced by the current value.
For example:
response.setHeader("Content_Type","text/html");

The above statement would give output as
Content_Type: text/html

sendRedirect(String):

sendRedirect method of response object is used to send a redirect response to the client temporarily by making use of redirect location URL given in parameter. 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.response.sendRedirect(String)

2 comments:

  1. The response object has page scope.

    ReplyDelete
  2. It encapsulates the response generated by the JSP to be send back to the client in response to the request. It is
    generated by the container and passed to the JSP as a parameter to the _jspService( ) method, where it is
    modified by the JSP.

    ReplyDelete

java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...