HTTP
Home ] Up ] [ HTTP ] A Simple Java Web Server ] CGI ] Java Servlets ] Apache Tomcat ] Tomcat Directory Structure ] Creating a Web Application ] Web Application Directory Structure ] What's a Servlet? ] Generating Other Content ] The Servlet Life Cycle ] Servlets & Form Data ] Request Headers ] CGI Variables ] Redirection ]

 

 

What is HTTP?

The HyperText Transfer Protocol is a stateless, TCP/IP-based protocol for communication over the World-Wide Web.  It is typically used between a client browser and a web server:

  1. The client browser issues a request to the web server.  The client:
    • Opens a TCP connection to the web server's HTTP port (usually port 80)
    • Sends an HTTP request message to the web server
  2. The web server:
    • Receives the request
    • Does the requested work
    • Returns the information requested in a response message back to the client
    • The connection is closed

The HTTP Request

The most common form of request from a browser is a GET.  In its simplest form, it looks like:

GET /index.html HTTP/1.0

a single line, where:

  • The first item on the line is called the method (here a GET)
  • The second item is the pathname of the requested file
  • The third item indicates we are using the HTTP protocol, and specifies the version.

A GET request can also include a request header.  Here's an example:

As you can see, this GET request was generated by Microsoft Internet Explorer 6.0, and there is lots of other information you could extract.

The HTTP Response

In response to the above HTTP request, the web server generates an HTTP response.

If the request was successfully satisfied by the web server, then the first line of the response will look like:

HTTP/1.0 200 OK

The 200 is the status code for success.  "OK" is a human-readable description of the meaning of 200.

Following the first line are a number of response header lines, which communicate a number of useful pieces of information about the server, and a description of the contents of the file that follows in the response. A blank line which separates the header from the file contents.  For example:

HTTP/1.0 200 OK
Server: Apache/1.3.9 (Win32)
Content-Type: text/html
Content-Length: 94

<HTML>
<HEAD>
<TITLE>HTTP Request Successful</TITLE>
</HEAD>
<BODY>
It Worked!
</BODY>
</HTML>



<----- Note the blank line here

HTTP Status Codes

If the web server encountered problems -- perhaps it could not find the requested file -- then the first line of the response contains one of a number of error status codes.  Status codes are organized numerically to indicate the level/type of error:

Code Range Category Description
1xx Informational Mostly experimental application codes.  HTTP/1.0 does not define any Informational status codes;  HTTP/1.1 does.
2xx Successful Request was received, understood and accepted.
3xx Redirection The server is redirecting the client to another URL.
4xx Client Error The request is improperly formatted, or cannot be fulfilled.
5xx Server Error A valid request was received, but the server cannot fulfill it.

Here are some actual status codes (this is not a complete list):

Status Code Description

200 OK

The request succeeded.

201 Created

The request was successful, and resulted in the creation of a new resource.

301 Moved Permanently

The resource has been moved permanently.  The Location header field specifies the new location.

302 Moved Temporarily

The resource has been moved temporarily.  The Location header field specifies the new location.

400 Bad Request

The request used invalid syntax.

401 Unauthorized

Authorization, such as username and password, is required to access this resource.  A WWW_Authenticate header field containing an authorization challenge is returned.  The web browser will normally display a username/password dialog box to the user

403 Forbidden

The server refuses to fulfill the response.

404 Not Found

No resource matching the requested URL exists on the server.

500 Internal Server Error

The server encountered an unexpected problem.

501 Not Implemented

The server does not implement the requested functionality.

 

 

The page was last updated February 19, 2008