Detect Device Type In Java Web Application

Detect Device Type In Java Web Application explain about identifying the device type such as desktop, tablet or mobile of client by accessing your Website.

For example, in your project you need to show 2 different versions of website. One is for mobile viewers and another for desktop viewers. For this you need to identify whether the request coming form a Mobile device. After detecting device type using user-agents string from HttpServletRequest. you can forward to different urls.

How to identify whether a user request is coming from Mobile/Tablet/Desktop?

For identifying the device type (whether the request is coming from mobile, tablet or desktop), you need to parse User-Agents string in your java web application, It is server side device detection.

We have following 3 approaches to detect the device types by parsing User-Agents.

1) By Using UADetector
2) By Using user-agent-utils
3) By Using Spring Framework

1) UADetector Example

UADetector is a Java library to identify over 190 different desktop and mobile browsers and 130 other User-Agents like feed readers, email clients and multimedia players. In addition, even more than 400 robots like BingBot, Googlebot or Yahoo Bot can be identified.

Reference -> http://uadetector.sourceforge.net

Required Libraries

Project Structure

Device Type Of Web Application Using UADetector

Detecting Device Type Of Web Application By Using UADetector

Here we are creating a servlet and manipulating the user-agents string from HttpServletRequest and identify the device type(mobile or desktop) that is sending the request.

package com.devicechecker;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;


/**
* This servlet is used to find the exact device type (desktop,mobile,tablet etc.) of client by scanning User-Agent string with the help of UADetector api
*/

@WebServlet("/DeviceCheckerServlet")
public class DeviceCheckerServlet extends HttpServlet {
   
private static final long serialVersionUID = 1L;

  
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
response.setContentType("text/html");
        response.setStatus
(HttpServletResponse.SC_OK);
        
        PrintWriter out = response.getWriter
();
        
       
// Get an UserAgentStringParser and analyze the requesting client
       
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        OperatingSystem agent = userAgent.getOperatingSystem
();
       
        out.append
("You're a <em>");
        out.append
(agent.getName());
        out.append
("</em> on <em>");
        out.append
(agent.getDeviceType().getName());
        out.append
("</em>!");
   
   
}
}
Output

From below, you can see that different requests leads to different output

Detect IPAD Device Type Using UADetector Detect IPhone Device Type Using UADetector Detect Tablet Type Using UADetector

2) user-agent-utils Example

It is Java user agent parser for processing user-agent strings. Can be used to handle http requests in real-time or to analyze log files.

Reference -> http://www.bitwalker.eu/software/user-agent-utils

Required Libraries

For using user-agent-utils, You need to have following classes (See below project structure for required classes)

  1. user-agent-utils

Project Structure

user-agent-utils Example

Detecting Device Type Of Web Application By Using user-agent-utils

Here we are creating a servlet we want to detect the device type(mobile or desktop) for the device that is sending the request.

package com.devicechecker;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;


/**
* This servlet is used to find the exact device type(desktop,mobile,tablet etc.) of client by scanning User-Agent string with the help of user-agent-utils api
*/

@WebServlet("/DeviceCheckerServlet")
public class DeviceCheckerServlet extends HttpServlet {
   
private static final long serialVersionUID = 1L;

  
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
response.setContentType("text/html");
        response.setStatus
(HttpServletResponse.SC_OK);
        
        PrintWriter out = response.getWriter
();
        
       
// Get an UserAgentStringParser and analyze the requesting client
       
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        OperatingSystem agent = userAgent.getOperatingSystem
();
       
        out.append
("You're a <em>");
        out.append
(agent.getName());
        out.append
("</em> on <em>");
        out.append
(agent.getDeviceType().getName());
        out.append
("</em>!");
   
   
}
}
Output

From below, you can see that different requests leads to different output

Detect IPAD Device Type Using UserAgentUtils Detect IPhone Device Type Using UserAgentUtils Detect Tablet Type Using UserAgentUtils

3) Detecting Device Type By Using SpringFramework

If you use spring framework, then you can use spring in-built feature. By adding spring-mobile-device-1.0.1.RELEASE.jar in your classpath.

I have already mentioned this in my previous tutorial, you can see here









Your email address will not be published. Required fields are marked *