Skip to main contentLogo

Command Palette

Search for a command to run...

Server, Handler, and Mapping — How it works behind the scenes

Published on
Apr 10, 2025
Server, Handler, and Mapping — How it works behind the scenes

Server

At the most basic level, a Server is a program or device that listens for and accepts connection requests from clients on a specific address (IP) and port on a network. In the web context, we typically refer to specialized software that processes requests coming via protocols like HTTP(S) (e.g., Jetty, Tomcat, Nginx, Apache HTTP Server).


How it works behind the scenes

Socket Listening

  • When the server program starts, it asks the operating system (OS) to open a TCP socket on a given IP:PORT pair.
  • The socket is bound to the address with the bind() system call.
  • With the listen() call, the server becomes ready to accept connections.

Connection Acceptance

  • When a client attempts to establish a TCP connection (3-way handshake), the OS adds this request to the listen queue.
  • The server program retrieves these connections with accept().
  • Each accept() call returns a new socket descriptor — this creates the communication channel between client and server.

Request Reading & Protocol Parsing

  • The server reads raw bytes from the new connection.
  • The data is in HTTP format (e.g., GET /index.html HTTP/1.1\r\nHost: example.com).
  • The HTTP parser analyzes:
    • HTTP method (GET, POST, etc.)
    • URI
    • HTTP version
    • Headers
    • Body (if any)

Concurrency Handling

Servers must be able to handle thousands of connections simultaneously. The following models are used:

a. Thread-Per-Connection

  • A thread is created for each connection.
  • Simple, but can waste resources with many connections.

b. Thread Pool

  • Uses a pre-created pool of threads.
  • For example: QueuedThreadPool in Jetty
  • More efficient resource utilization.

c. Event-Driven / Non-blocking I/O (NIO)`

  • Manages thousands of connections with one or a few threads.
  • Uses OS event mechanisms (epoll, kqueue, etc.).
  • Popular examples: Nginx, Node.js, Jetty NIO, Tomcat NIO, Undertow

How Server works

The server’s main job:

  • Accept incoming communication requests
  • Determine the protocol (HTTP parsing)
  • Forward the request to the appropriate component
  • Send the response back to the client

Main focus: network communication, protocol analysis, parallel processing.


Handler

After the server parses the raw request, the answer to “what should I do with this request?” is found in Handler components.

A handler is a block of code that processes the request, modifies it, generates a response, or forwards it to another handler. It’s a core concept in modular servers like Jetty.


How it works behind the scenes

Chain/Tree Structure

  • Handlers are configured as a chain of responsibility or a tree structure.
  • The request is processed sequentially through this structure.

handle() Method

  • Each handler has a handle(Request, Response) method.
  • The server forwards the request to the first handler with this method.

Responsibility and Delegation

A handler can:

  • Generate the response itself (e.g., ResourceHandler).
  • Perform an operation and pass it to the next handler (e.g., RequestLogHandler, SecurityHandler).
  • Dispatch to child handlers (e.g., ContextHandler).
  • Mark processing state — if the response has already been created, subsequent handlers are skipped.

How Handler works

Handlers break down web requests into small, modular, reusable pieces. Each performs a specific function:

  • Logging
  • Security checks
  • Session management
  • Static file serving
  • Servlet processing, etc.

This model makes the server flexible and extensible.


Mapping

Servers receive various URLs like /users, /products/123, /admin/login. Mapping is the process of routing these requests to the correct handler or block of code.


How it works behind the scenes

URL Pattern Matching

Request URLs are matched against predefined patterns.

Matching types:

  • Exact Match/login only with /login
  • Prefix Match/admin/*/admin/users, /admin/settings
  • Suffix Match.do/edit.do, /delete.do
  • Root Match/ → Default fallback pattern

Configuration Sources

URL patterns can be defined in various ways:

  • web.xml → with <servlet-mapping> in Java EE
  • Annotations@WebServlet("/users")
  • Programmatic Configuration → e.g., via ServletContextHandler.addServlet() in code

Handler Responsibility

Mapping is usually managed by a dedicated handler:

  • In Jetty → ContextHandler, ServletHandler

How Mapping works

Mapping is the key mechanism for routing requests to the right place. While handlers control the flow, mapping determines the route of this flow.


Thanks for reading.