- Hypertext Transfer Protocol
- The Structure of an HTTP Message
- The Structure of an HTTP Request
- The Structure of an HTTP Response
- Testing an HTTP Connection
- Passing Request Parameters
- Client Page Caching
- URI Redirection and Page Refresh
- Persistent Connections
- Using HTTP from Within a Java Program
Every page request and response requires a new network connection to be set up and torn down. This is a large overhead, especially when requesting lots of small files, such as might occur on a Web page with several embedded images.
HTTP enables a client and server to negotiate to keep a single network connection alive to handle multiple requests and responses. HTTP 1.0 requires the client to explicitly ask to keep a connection alive, whereas HTTP 1.1 assumes the connection is persistent unless the client or server indicates otherwise. The Connection header field is used to negotiate connection persistence:
An HTTP 1.1 client or server can close a persistent connection by sending the Connection: close header with the request or response.
An HTTP 1.0 client can ask the server to keep the connection alive using the Connection: Keep-Alive header.
Persistent connections will only stay open for a specified period of time. A server will close a connection after the timeout expires to prevent clients from hogging a network connection and reducing server throughput. Tomcat defines the connection timeout in the server.xml file, discussed in Chapter 16, "Configuring Tomcat."
The only constraint on a persistent connection is ensuring that every request and response message indicates the size of the message body using the Content-Length header field. If the content length is not specified, the client or server can only determine the end of the body when the network connection is closed. If the request or response does not have a body, the Content-Length header can be omitted.
Persistent connections are a good technique for improving Web server performance, and can be supported when developing servlets by ensuring that the servlet specifies the length of the content it's returning to the client. This is discussed in greater detail in Chapter 7.