Detecting Device Type Using LiteDeviceResolver

Example of browser\’s device type detection using DeviceResolver & LiteDeviceResolver classes of spring device detection library in Java Servlet application.

detecting-device-servlet

In Java web application many time we need to check client device type like normal PC, mobile or tablet device. For this spring has very good library for spring-based application. But that library can also be used in normal servlet application.

Library name is spring-mobile-device.x.x.x.x.jar and you can download from here.

Maven entry

<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>

Now in you application you can get device type using HttpServletRequest object input as given below.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DeviceResolver resolver = new LiteDeviceResolver();
Device device = resolver.resolveDevice(request);
String deviceType = "unknown";
if (device.isNormal()) {
deviceType = "normal";
} else if (device.isMobile()) {
deviceType = "mobile";
} else if (device.isTablet()) {
deviceType = "tablet";
}
response.getOutputStream().print("Hello " + deviceType + " browser!");
}
public interface DeviceResolver

Service interface for resolving Devices that originate web requests with the application.

Here is output

device-detection