Friday, 21 August 2015

Request and Response Interface

Details about the Request interface and the Response interface.

  • HttpServlet gets request from a client (browser) on the HttpServletRequest interface and can return response to the client through the HttpServletResponse interface in all the doXXXX methods.
  • Here is some of most used HttpServletRequest Methods:

    MethodDescription
    public Enumeration getParameterNames()Names of the parameters contained in this request.
    public String[] getParameterValues(String name)Used when the named parameter may have multiple values.
    public Map getParameterMap()Returns all the parameters stored in a Map object.
    public BufferedReader getReader()This will retrieve the body of a request as characters into a BufferedReader.
    public ServletInputStream getInputStream()This will retrieve the body of a request as binary data into a ServletInputStream.
    public Enumeration getHeaderNames()Returns an enumeration of all the header names this request contains.
    public String getQueryString()This will return the query string that is contained in the request URL after the path.
    public RequestDispatcher getRequestDispatcher(String path)A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response.
  • You have to use either the getReader() method or the getInputStream(). You cannot combine to use these for the same request.
  • Here is some of most used HttpServletResponse Methods:

    MethodDescription
    public PrintWriter getWriter()This will return a PrintWriter object that can send character text to the client.
    public ServletOutputStream getOutputStream()This will return a ServletOutputStream suitable for writing binary data in the response.
    public addHeader(String name, String value)This will add a property, which is a String name with a String value to the response header.
    public addDateHeader(String name, long date)This will add a property, which is a String name with a long date value to the response header.
    public void sendRedirect(String location)This will send a temporary redirect response to the client using the specified redirect location URL.
    public Enumeration getHeaderNames()Returns an enumeration of all the header names this request contains.
  • You have to use either the getWriter() method or the getOutputStream(). You cannot combine to use these for the same response

No comments:

Post a Comment