Overview of Servlets and JSP Overview of HTTP ©SoftMoore Consulting
Overview of Servlets and JSP HTTP Hypertext Transfer Protocol Specification: http://www.w3.org/Protocols/rfc2616/rfc2616.html Set of rules for transferring resources (files, images, etc.) over the web A resource on the web is usually identified by a URL (Uniform Resource Locator) Characteristics simple stateless text-based (originally, but not HTTP/2) request/response protocol ©SoftMoore Consulting
Structure of an HTTP Transaction Overview of Servlets and JSP Structure of an HTTP Transaction HTTP client (e.g., a browser) opens a connection and sends a request message to an HTTP server. Server formulates and returns a response message, usually containing the resource that was requested. In HTTP/1.0, after delivering the response, the server closed the connection. Connection information was not maintained between transactions. HTTP/1.1 added persistent connections connection can remain open after receiving a response and can be re-used for the next request ©SoftMoore Consulting
HTTP Example Using HTTP 1.0 Overview of Servlets and JSP HTTP Example Using HTTP 1.0 From the command prompt, enter the following commands: telnet set localecho open macs.citadel.edu 80 GET /moorej/hello.html HTTP/1.0 <enter> Note: Telnet is not installed by default on Windows. To turn it on, go to Control Panel -> All Control Panel Items -> Programs and Features. Click on “Turn Windows features on or off” and check the box for Telnet Client. ©SoftMoore Consulting
HTTP Example Using HTTP 1.1 Overview of Servlets and JSP HTTP Example Using HTTP 1.1 From the command prompt, enter the following commands: telnet set localecho open macs.citadel.edu 80 GET /moorej/hello.html HTTP/1.1 Host: macs.citadel.edu <enter> With HTTP 1.1, a single web server can host several domains. Therefore, every HTTP request must specify the host name (and possibly port if different from 80). ©SoftMoore Consulting
Overview of Servlets and JSP HTTP Message Format Both request and response messages have a similar format: an initial line zero or more lines known as headers a blank line an optional message body (e.g. a file, query data, binary data, etc.) ©SoftMoore Consulting
Overview of Servlets and JSP Initial Request Line The initial line of a request has three parts, separated by spaces: Method name GET is the most common HTTP method Other methods include POST and HEAD Method names are always uppercase Local path of the requested resource the request URI (a URI is like a URL, but more general) Version of HTTP being used Always takes the form “HTTP/x.x” ©SoftMoore Consulting
Initial Response Line (a.k.a. the Status Line) Overview of Servlets and JSP Initial Response Line (a.k.a. the Status Line) The initial line of a response also has three parts, separated by spaces: The HTTP version same format as the initial request line, “HTTP/x.x” A response status code that gives the result of the request Examples: 200 or 404 An English phrase describing the status code. Examples: “OK” or “Not Found” Example: HTTP/1.0 200 OK ©SoftMoore Consulting
Overview of Servlets and JSP Status Code Three-digit integer First digit identifies the general category of response 1xx indicates an informational message only 2xx indicates success of some kind 3xx redirects the client to another URL 4xx indicates an error on the client's part 5xx indicates an error on the server's part ©SoftMoore Consulting
Overview of Servlets and JSP Common Status Codes 200 OK request succeeded; resource is returned in the message body 404 Not Found requested resource doesn’t exist 301 Moved Permanently 302 Moved Temporarily 303 See Other (HTTP 1.1 only) resource has moved to the URL given in the “Location” response header; should be automatically retrieved by the client 500 Server Error unexpected server error ©SoftMoore Consulting
Overview of Servlets and JSP HTTP Methods HTTP defines a set of request messages called methods (not to be confused with Java methods) GET POST PUT DELETE HEAD Others (CONNECT, TRACE. etc.) ©SoftMoore
Overview of Servlets and JSP The HTTP GET Method Requests information from a web server Example GET /HelloUser.jsp?name=John HTTP/1.1 Host: www.softmoore.com:80 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 Accept: text/xml,application/xml,application/xhtml+xml, text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Note: Each header is on a single line (no line breaks). ©SoftMoore
Using HTTP GET Requests Overview of Servlets and JSP Using HTTP GET Requests The handling of a GET method is expected to be safe: it does not have any side effects for which users are held responsible idempotent: it can safely be repeated. This allows a client to repeat a GET request without penalty. Intuitively, GET should be “looking without touching.” If processing has side effects, use another HTTP method, such as POST. ©SoftMoore
Important Limitation on HTTP GET Requests Overview of Servlets and JSP Important Limitation on HTTP GET Requests Most web servers limit how much data can be passed as part of the URL name (usually a few hundred bytes). If more data must be passed between the client and the server, the HTTP POST method can be used instead. data as part of the URL ©SoftMoore
Example: HTTP Response to a GET Method Overview of Servlets and JSP Example: HTTP Response to a GET Method HTTP/1.1 200 Document follows Date: Tue, 14 Apr 2007 09:25:19 PST Server: JWS/1.1 Last-modified: Mon, 17 Jun 2005 21:53:08 GMT Content-type: text/html Content-length: 4435 <4435 bytes worth of data -- the document body> ©SoftMoore
Overview of Servlets and JSP The HEAD Method Similar to the HTTP GET method Server returns only the header information. HEAD is often used to check the following: last-modified date of a document on the server for caching purposes size of a document before downloading server type type of the requested document ©SoftMoore
Overview of Servlets and JSP The POST Method Allows a client to send data to the server Passes all of its parameter data in an input stream Unlike the GET method, POST may cause side effects is not required to be repeatable Several purposes posting information to a newsgroup adding entries to a web site's guest book passing more information than a GET request allows ©SoftMoore
Overview of Servlets and JSP Example: POST Request POST /servlet/MyServlet HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT) Host: www.softmoore.com Accept: image/gif, image/x-xbitmap, image/jpeg, */ Content-type: application/x-www-form-urlencoded Content-length: 39 name=John&company=SoftMoore%20Consulting Note: A blank line signals the end of the POST request header and the beginning of the extended information. ©SoftMoore
HTTP/2 Second major version of the HTTP network protocol Approved in February 2015 as the successor to HTTP 1.1. which was standardized in 1997. Based on SPDY, an HTTP-compatible protocol developed by Google and supported in most browsers. ©SoftMoore Consulting
HTTP/2 Features Binary, multiplexed network protocol no longer text-based Basic protocol unit of HTTP/2 is a frame. Frames are exchanged over a TCP connection instead of text-based messages. Negotiation mechanism allows use of HTTP 1.1, 2.0, etc. Maintains high-level compatibility with HTTP 1.1 HTTP methods, status codes, default port numbers, and semantics are not changed. Loads page elements in parallel over a single TCP connection HTTP/1.1 used multiple parallel connections for downloading all the elements on a given web page (e.g., images). ©SoftMoore Consulting