How to detect mobile (iOS and Android) using JSP/Java?

21,498

Solution 1

Best way would probably be with the User Agent string. There's actually a pretty similar question on SO already, at least for iOS/Safari. Note that there are other browsers on iOS so you will need to look for their user agent strings as well.

Alot of UA strings listed on this site.

How do I detect Mobile Safari server side using PHP?

Solution 2

A very simple solution would be:

<%
  String userAgent = request.getHeader("user-agent");
  if (userAgent.matches(".*Android.*"))
  {
    out.print("You're an Android!");
  }
  else
  {
    out.print("You're something else..."); // iOS
  }
%>

Because of the very short else-statement, this should be used only if you serve no more than iOS and Android.

Solution 3

You can use the User Agent to determine if IOS or Android is being used. Just look for the appropriate keywords such as "Android" or "iPhone" or "iPad"

Share:
21,498
jrutter
Author by

jrutter

Smashing jQuery Author and Front-end developer.

Updated on January 09, 2020

Comments

  • jrutter
    jrutter over 4 years

    Just wondering if anyone has come across this?

    Basically, Im looking to detect for iOS and Android using JSP and to be able to conditionally add CSS and JS files to the page.

    Any ideas?