Wednesday, December 14, 2011

What is HTTP ? How does HTTP work ?

 " Hypertext Transfer Protocol " is a request - response protocol which is used by webmasters and other clients to access a web server.
Basics of HTTP

  • HTTP ( Hypertext Transfer Protocol ) is a protocol which is used by a www ( e.g a Browser ) to send a request to a web server.
  • HTTP is a Request - Response oriented protocol.
  • A HTTP request consists of 
  1. Request method
  2. URI
  3. Header fields
  4. Body ( which can be empty )
  •  A HTTP response consists of
  1.  Result code
  2. Header fields
  3. Body
 Overall Process

  • The service method of HttpServlet dispatches a request to different java methods for different HTTP request methods.
  • It recognizes the standard HTTP/1.1 methods and It should not be overridden in subclasses unless you need to implement additional methods.
  • The recognized methods are GET, HEAD, PUT, POST, DELETE, OPTIONS and TRACE. Other methods are answered with a Bad request HTTP error.
  • An HTTP method xxx is dispatched to a Java method doxxx ie. GET ==> doGET.
  • The doOptions & doTrace methods hava suitable default implementations & are usually not overridden.
  • The HEAD method ( similar to GET method ) is performed by calling doGet & ignores any o/p that is written by this method.
  • This leaves us with doGet, doPut, doPost & doDelete. whose default implementations return a Bad request in HttpServlet.
  • HttpServlet overrides one or more of the remaining methods by its subclass to provide a meaningful implementation.
The Request data is passed to all methods through the first argument of type HttpServletRequest ( A subclass of ServletRequest class ).
The Response data created is created with methods of the second argument of type HttpServletResponse ( A subclass of ServletResponse class ).
 How GET and POST methods work ?

when a client request a URL through a web browser, the GET method is used for the request.
 GET method

  • A GET request does not have a body ( i.e  the body is empty ).
  • where as the response should contain a body with the response data & header fields which describe the body ( like content-type & content-encoding )
  • when we send a HTML form, using GET method( request) the parameters are encoded in the URL ( i.e The data is sent through URL ).
PUT method

  • PUT method(request) is used by HTML editors & Upload tools to upload resources to a web server.
  • when we send a HTML form, using PUT method(request) the parameters are transmitted in the body ( i.e The data is sent through Body ).



No comments:

Post a Comment