I did a google search and found that PHP uses:

$_SERVER, which would give the following result, should I visit a page:

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16

is their any functionality in java that will do the same??

thanx

Dani AI

Generated

As pointed out, the browser identifies itself in the HTTP request headers. In a Java web app you read that header from the incoming request and then decide whether to treat the visitor as a mobile client. That works, but the header text varies a lot, can be spoofed, and tablets/desktops sometimes look like mobiles, so choose the approach that matches your needs.

A simple, pragmatic server-side check is to parse the header string for common mobile tokens. For example, given the header value in a variable ua, a small utility can do a case‑insensitive token search:

/* ua is the browser header string from the HTTP request */
public static boolean isMobile(String ua) {
    if (ua == null) return false;
    java.util.regex.Pattern p = java.util.regex.Pattern.compile(
        "(?i)\\b(?:android|iphone|ipod|ipad|blackberry|bb\\d+|windows phone|iemobile|opera mini|opera mobi|mobile|fennec|kindle)\\b");
    return p.matcher(ua).find();
}

Notes and best practices:

  • Tune the token list for phones vs tablets (omit ipad if you want tablets treated as desktop). Log real UA strings you see so you can refine rules.
  • For production use prefer maintained device databases/libraries: WURFL, 51Degrees, DeviceAtlas or the community UA-Parser Java.
  • Consider modern alternatives: client hints / navigator.userAgentData and responsive CSS media queries (see MDN: userAgentData and media queries).
  • Cache parse results, allow a user override (view desktop/site mobile), and avoid hard redirects based only on UA because of false positives.

Recommended Answers

All 3 Replies

request.getHeader("User-Agent"

request.getHeader("User-Agent"

thanx man

No option to edit posts here? ) missing, and I can't see any EDIT button to rectify it :( . Mark as solved if your got the solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.