Class HelloWorldHandler
This handler demonstrates the basic structure of a GET request handler in the framework.
It extends MethodeBasedHandler to provide HTTP method-specific handling and
implements only the GET method, making it suitable for simple read-only endpoints.
The handler always returns the same static HTML content regardless of the request parameters, making it ideal for:
- Testing server functionality and route configuration
- Demonstrating basic handler implementation patterns
- Serving simple static content without file system access
- Health check or status endpoints
Usage example:
// Register in router configuration
router.register("/hello", new HelloWorldHandler());
// Access via HTTP GET
GET /hello HTTP/1.1
Host: example.com
// Response:
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 22
<h1>Hello, World!</h1>
Supported HTTP methods:
- GET: Returns the "Hello, World!" HTML content (implemented)
- HEAD: Returns headers only without body (inherited from parent)
- POST, PUT, DELETE, etc.: Returns 405 Method Not Allowed (inherited from parent)
Thread-safety: This handler is stateless and thread-safe. Multiple threads can
safely invoke handleGetRequest(HTTPRequest) concurrently.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected HTTPResponsehandleGetRequest(HTTPRequest request) Handles HTTP GET requests by returning a simple "Hello, World!" HTML response.Methods inherited from class MethodeBasedHandler
handle, handleDeleteRequest, handleHeadRequest, handlePostRequest, handlePutRequestMethods inherited from class RequestHandler
handle
-
Constructor Details
-
HelloWorldHandler
public HelloWorldHandler()
-
-
Method Details
-
handleGetRequest
Handles HTTP GET requests by returning a simple "Hello, World!" HTML response.This method creates a standard HTTP 200 OK response with HTML content displaying a greeting message. The response includes proper Content-Type and Content-Length headers automatically set by the
HTTPResponse.setBody(byte[], String)method.Response characteristics:
- Status: 200 OK - Indicates successful request processing
- Content-Type: text/html; charset=UTF-8 - HTML content with UTF-8 encoding
- Body: "<h1>Hello, World!</h1>" - Simple HTML heading
- Encoding: UTF-8 - Ensures proper character representation
The method ignores all request parameters, headers, and body content, always returning the same static response. This makes it deterministic and predictable for testing purposes.
- Overrides:
handleGetRequestin classMethodeBasedHandler- Parameters:
request- The HTTP GET request to handle. The request content is not used but must not be null.- Returns:
- An HTTPResponse with status 200 containing the "Hello, World!" HTML message. Never returns null.
- Throws:
Exception- This implementation does not throw exceptions under normal circumstances, but the signature allows for future enhancements that might require exception handling (e.g., template rendering, database access).
-