Handling Requests
Handling Requests
Every meaningful servlet begins the same way: the container hands you an HttpServletRequest object and expects you to extract whatever data the client sent. That single object is the gateway to query-string parameters, form fields, HTTP headers, path information, cookies, and the raw request body. Understanding how to read from it accurately — and safely — is one of the most practical skills in server-side Java.
The HttpServletRequest Object
HttpServletRequest extends ServletRequest and gives you everything HTTP-specific. The container populates it before calling your doGet or doPost method, so it is fully ready to use as soon as you receive it. You never construct this object yourself.
The most commonly used groups of methods are:
- Parameter access — query-string and form-body values.
- Header access — HTTP headers sent by the browser or upstream proxy.
- Request metadata — method, URI, context path, remote address.
- Attribute store — a
Map-like bag for passing objects between components during one request.
Reading Query-String and Form Parameters
Whether data arrives on the URL (?name=Alice&age=30) or in a form body with Content-Type: application/x-www-form-urlencoded, the servlet API presents it identically through getParameter. This uniformity is intentional: you describe what you need, not where it came from.
getParameter returns null, never an empty string, when a parameter is missing entirely. Always null-check before calling methods on the result. Skipping this is one of the most common sources of NullPointerException in servlet code.
Multi-Value Parameters
A checkbox group or a repeated URL parameter (e.g. ?tag=java&tag=web&tag=servlet) sends the same key multiple times. getParameter returns only the first value in that case. Use getParameterValues to retrieve all of them as a String[].
If you want a full picture of every parameter name the client sent, getParameterMap() returns a Map<String, String[]> — each key maps to the complete array of values for that name. This is useful for logging, debugging, or building generic form processors.
%20 → space) automatically. However, it uses the request's declared character encoding to do so. For non-ASCII form data, always call req.setCharacterEncoding("UTF-8") before the first call to getParameter — once the parameters are read, changing the encoding has no effect.
Reading HTTP Headers
HTTP headers carry metadata the client attaches to every request: the browser name, accepted content types, authentication tokens, cache directives, and more. You read them by name through getHeader(String name). Header names are case-insensitive by the HTTP specification, and the servlet API honours that.
When you need to inspect every header the client sent, use getHeaderNames(), which returns an Enumeration<String>:
User-Agent or X-Forwarded-For headers for security decisions. Both are trivially forged by any HTTP client. Use them only for analytics, logging, or content negotiation — never for authentication or access control.
Request Metadata
Beyond parameters and headers, HttpServletRequest exposes structural information about the request itself. These methods are essential when writing generic filters, logging middleware, or URL routing logic.
Reading the Raw Request Body
For POST or PUT requests carrying JSON, XML, or other non-form content types, the body arrives as a byte stream. You access it through getInputStream() (for binary) or getReader() (for text). The two are mutually exclusive: calling both in the same request throws an IllegalStateException.
Putting It Together: A Realistic Search Endpoint
Here is a servlet that combines everything above — parameters, headers, and metadata — into a pattern you will recognise from real applications:
Summary
HttpServletRequest is your single point of contact with everything the client sent. Use getParameter for single values and getParameterValues when a key can repeat. Always null-check and validate before parsing. Read headers with getHeader — and remember they are advisory, not authoritative, for security purposes. For non-form bodies, use getReader() or getInputStream(), being aware that reading is destructive. The next lesson covers HttpServletResponse: once you have read the request correctly, you will know exactly what to write back.