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 ->

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 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

public class DeviceCheckerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

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

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

1 2 Next

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