03 HTTP
Protocol
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. It is a stateless request-reponse protocol used to access data on another computer.
Versions
Request
HTTP requests are messages sent by the client to initiate an action on the server. A request consists of:
- A request line
- HTTP method
- Request target (absolute path)
- HTTP version
- Headers (optional key-value pairs)
- An empty line
- A message body (optional)
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/html
Methods
The following HTTP methods are defined:
| Method | Request Body | Safe | Idempotent | Cacheable |
|---|---|---|---|---|
GET | Optional | Yes | Yes | Yes |
HEAD | Optional | Yes | Yes | Yes |
POST | Yes | No | No | Yes |
PUT | Yes | No | Yes | No |
DELETE | Optional | No | Yes | No |
PATCH | Yes | No | No | No |
OPTIONS | Optional | Yes | Yes | No |
CONNECT | Optional | No | No | No |
TRACE | No | Yes | Yes | No |
Headers
HTTP headers are key-value pairs that provide additional information about the request or response.
Common headers include:
Accept: Media types accepted by the clientAccept-Encoding: Content encodings accepted by the clientAccept-Language: Human languages accepted by the clientAuthorization: Credentials for authenticationCache-Control: Cache directivesContent-Type: Media type of the bodyCookie: Cookies sent by the clientHost: Domain name of the serverLocation: Redirect locationReferer: Referrer URL (spelling mistake in the spec)User-Agent: Information about the client
Response
HTTP responses are messages sent by the server in response to a client’s request. A response consists of:
- A status line
- HTTP version
- Status code
- Message
- Headers (optional key-value pairs)
- An empty line
- A message body (optional)
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 35
<!DOCTYPE html><html>Hello, World!</html>
Java Webserver
An example web server implementation in Java that returns the current date.
public class HttpDemoServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(80), 0);
server.createContext("/date", new DateHandler());
server.start();
}
}
public class DateHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().add("Content-Type", "text/html");
String response = "<b>" + LocalDateTime.now() + "</b> for " + exchange.getRequestURI();
exchange.sendResponseHeaders(200, response.length());
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes());
}
}
}
Java Servlet
Java Servlets are server-side Java components that generate dynamic content. They are part of the Java EE (Enterprise Edition) platform.
Lifecycle Methods
void init(ServletConfig config): Called by the servlet container to indicate that the servlet is being placed into service.void destroy(): Called by the servlet container to indicate that the servlet is being taken out of service.void service(ServletRequest req, ServletResponse res): Called by the servlet container to allow the servlet to respond to a request.
HTTP Servlet
The HttpServlet class extends the GenericServlet class and provides methods to handle HTTP-specific requests.
The service method dispatches requests to the appropriate doXXX method based on the HTTP method.
void doGet(HttpServletRequest req, HttpServletResponse res)void doPost(HttpServletRequest req, HttpServletResponse res)void doPut(HttpServletRequest req, HttpServletResponse res)void doDelete(HttpServletRequest req, HttpServletResponse res)void doHead(HttpServletRequest req, HttpServletResponse res)void doOptions(HttpServletRequest req, HttpServletResponse res)void doTrace(HttpServletRequest req, HttpServletResponse res)
Example
Currency converter web application using a servlet.