Parse and analyze User-Agent strings to identify browser, OS, and device information.
A User-Agent (UA) string is an HTTP request header that browsers send with every request, identifying the client software: browser name, version, rendering engine, and operating system. Servers use this for analytics, content negotiation, feature detection, and logging. UA strings have a notoriously complex history β virtually every browser includes "Mozilla/5.0" for compatibility with early Netscape-aware servers, even though it meaninglessly identifies nothing.
Chrome includes "Safari" in its UA; Safari includes "like Gecko". This compatibility theater makes manual parsing brittle, which is why dedicated parsing libraries are necessary for reliable results.
(Windows NT 10.0; Win64; x64) or (iPhone; CPU iPhone OS 17_0 like Mac OS X)AppleWebKit/537.36 (Chromium-based), Gecko/20100101 (Firefox)Chrome/120.0.0.0, Firefox/121.0, Safari/604.1Mobile in the UA stringGooglebot/2.1, bingbot/2.0Server-side UA-based browser detection for feature decisions is considered an anti-pattern in modern web development. UA strings can be spoofed, are increasingly frozen to prevent tracking, and do not reliably indicate capability. The recommended approach is feature detection: test whether a specific API exists in JavaScript (if ('serviceWorker' in navigator)) rather than inferring capability from the browser name. Legitimate uses for UA parsing: analytics, bot detection, logging, and serving different content variants based on detected device type.
Historical inertia. Early web servers served richer content only to Netscape (Mozilla). Internet Explorer added "Mozilla" to get the same content. All browsers followed suit. Today, "Mozilla/5.0" is a meaningless compatibility token in Chrome, Firefox, Safari, and Edge β it cannot identify any specific browser.
Use the uap-java (ua-parser) library. Add the dependency, then Client c = new Parser().parse(uaString). Access c.userAgent.family (browser name), c.os.family (OS), and c.device.family (device type). The ua-parser project maintains a community-updated regex file covering thousands of UAs. Alternatively, use user-agent-utils for a simpler API.
User-Agent Client Hints (UA-CH) is a newer mechanism that replaces the monolithic UA string with structured, opt-in hints. Servers request specific information via the Accept-CH header; browsers send only what is requested. This reduces passive fingerprinting while still allowing legitimate UA detection. Chrome has been progressively "freezing" its UA string to standardize certain tokens and encourage migration to UA-CH.