Detecting Mobile Devices Using Java

Thanks to the magical powers of the Internet, a kind fan of the PHP scripts for detecting mobile devices ported the code to Java. So for those folks using Java, here is a fantastic new option. And of course -- it's free!

For more detailed information about the caveats of attempting to detect mobile devices based on User Agent strings, please see our article on PHP scripting.

Quick apologies: I'm not as versed in Java as JavaScript and PHP, so any errors introduced in the code are mine. Satish, the gentleman who ported the PHP code to Java, did a great job! Since he sent the code to me, I've made a few modifications which are commented in the source code. Please accept my apologies in advance for any errors. And if you find any, please let me know!



As with PHP and JavaScript code, the basic concept is to do the following:

  1. Instantiate the class in a JSP page. (Some people may wish to do this in a Struts Action class.)
  2. Detect whether the current web site visitor is using a mobile device;
  3. If yes, redirect to a mobile-optimized page. (Or similar type of custom action for mobile devices.)


Introducing the "UAgentInfo" Class

The Java class called "" encapsulates all of the logic for detecting mobile devices. This class is easy to use and its API is highly modularized so that you can detect broad classes of devices (such as smartphones or WAP/WMP-capable devices) and specific platforms (such as the iPhone/iPod Touch, Symbian S60 or BlackBerry).

Here is a sample block of the code so you can see how it is organized:

// The UAgentInfo class encapsulates information about
//   a browser's connection to your web site. 
//   The object's detection methods return booleans: true or false.
public class UAgentInfo
   //Stores some info about the browser and device.
   private String userAgent;

   //Stores info about what content formats the browser can display.
   private String httpAccept; 

   // A long list of strings which provide clues 
   //   about devices and capabilities.
   public static final String deviceIphone = "iphone";

   // [ SNIP! Other variables snipped out ] 

   //The constructor. Initializes several default variables.
   public UAgentInfo(String userAgent, String httpAccept) {
        if (userAgent != null) {
            this.userAgent = userAgent.toLowerCase();
        if (httpAccept != null) {
            this.httpAccept = httpAccept.toLowerCase();

   //Returns the contents of the User Agent value, in lower case.
   public String getUserAgent()
       return userAgent;

   // Detects if the current device is an iPhone.
   public boolean detectIphone()
        //The iPod touch says it's an iPhone! So let's disambiguate.
        if (userAgent.indexOf(deviceIphone) != -1 && !detectIpod()) {
            return true;
        return false;

   // [ SNIP! Other functions snipped out ] 



Using the "UAgentInfo" Class

First, instantiate the UAgentInfo object, then call one of its functions. The device detection functions return booleans: true or false. It's as simple as that! Here's an example:

//1. Instantiate the object to do our testing with.
<%@page import=""%>

    //2. Initialize the browser info variables
    String userAgent = request.getHeader("User-Agent");
    String httpAccept = request.getHeader("Accept");

    //3. Create the UAgentInfo object
    UAgentInfo detector = new UAgentInfo(userAgent, httpAccept);

    //4. Detect whether the visitor is using a mobile device.
    //   For example, if it's an iPhone, redirect them to the  
    //   iPhone-optimized version of your web site.
    if (detector.detectIphoneOrIpod()) {
    } else {


Introducing MobileESP!

Since the original publication of this article in 2008, we've seen interest grow dramatically for this easy-to-use mobile detection library for web site publishers. As a result, we have spun this code library off into a separate free open source (Apace 2 license) code library called MobileESP. We've continuously expanded the capabilities of the PHP code library as new devices came out. So, for the latest information, including updates, bug fixes, new features, and more, please see the project web site: Here are a few quick links:

  • Get MobileESP: Download the latest version of the MobileESP code libraries! Learn more.
  • The MobileESP API: See the complete list of mobile detection functions which are largely consistent across supported web technologies (PHP, Java, APS.NET, Ruby, and JavaScript).
  • Live MobileESP API Demos: Want to see how the MobileESP APIs work with your device? Check out the PHP & JavaScript live demos!
  • Download Samples: There are several sets of sample web pages you can download to see how easy it is to integrate MobileESP into your web site. Learn more.
  • License Information: MobileESP is free to use, though donations are greatly appreciated and encouraged. For more information, please visit the Licensing page on the MobileESP web site.



MobileESP is easy to implement and generally great if you're primarily concerned about the class of device (e.g., smartphone or not, phone vs. tablet), or the mobile platform (e.g., iPhone or Symbian S60). If you need detailed device information or usage metrics, you may wish to check out WURFL (free!) or


About the Contributor

Our gratitude to Satish K. Nookala, who wrote the initial port of the PHP code to Java. Unfortunately, Satish doesn't have a web site where you can learn more about his great talents, but here's a brief bio in his own words:

"I am a Java programmer and systems analyst working for an Indian software company (right now working at a client location in USA). I have been coding in Java for the last 7 years or so. Also did coding in PERL, PHP, and Visual Basic. I love UNIX and whenever I find some time, I try to explore Linux. :-)"


Support MobileESP!

If you find the MobileESP code library useful, please consider donating so we can purchase additional devices on our testing wishlist! Donations of $25-50 are greatly appreciated!


MobileESP Project

Learn more about the new MobileESP project!

We've spun off our mobile device detection code into a FREE open source project called MobileESP! Learn more about it on the new project web site.

Please Donate

If you like and use this code, please consider donating. Any amount is appreciated!

(Please donate -- help us purchase more devices on our testing wishlist!)

About Us

Hand Interactive offers user experience, usability, and business strategy expertise. We're passionate about crafting engaging experiences for mobile, desktop, and web users.

Contact us for more information.