java
27 lines · 6 steps
Parsing an HTTP Cookie header in Java
Split a raw Cookie header into an ordered map of names to decoded values, defensively skipping malformed pieces.
Explained by
highlit
1public static Map<String, String> parseCookieHeader(String header) {
2 Map<String, String> cookies = new LinkedHashMap<>();
3 if (header == null || header.isBlank()) {
4 return cookies;
5 }
6
7 for (String pair : header.split(";")) {
8 int eq = pair.indexOf('=');
9 if (eq < 0) {
10 continue;
11 }
12
13 String name = pair.substring(0, eq).trim();
14 if (name.isEmpty()) {
15 continue;
16 }
17
18 String value = pair.substring(eq + 1).trim();
19 if (value.length() >= 2 && value.startsWith("\"") && value.endsWith("\"")) {
20 value = value.substring(1, value.length() - 1);
21 }
22
23 cookies.putIfAbsent(name, URLDecoder.decode(value, StandardCharsets.UTF_8));
24 }
25
26 return cookies;
27}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Guard against null and empty input up front so the happy path stays clean.
- 2Skip malformed entries with continue instead of throwing, keeping the parser tolerant of real-world headers.
- 3Preserving insertion order and honoring first-wins semantics matters when duplicate cookie names appear.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 steps
go
package httputil import ( "net"
Safely extracting the real client IP in Go
security
http
ip-spoofing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/parsing-an-http-cookie-header-in-java-explained-java-98a1/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.