How To Detect Device Type Using Spring

Detect Device Type Using Spring explaining about identifying the device type of a client (such as desktop, tablet or mobile) accessing your Website by using spring framework.

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

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 by scanning user-agents string from java HttpServletRequest. you can forward to different urls.

In our application also, we need to have this feature. Fortunately in our application using spring as dependency. By adding spring-mobile-device-1.0.1.RELEASE.jar, we could able to achieve what we want

Detect user is from Mobile/Tablet/Desktop Using Spring Framework?

In this example we are identifying the device type such as desktop, tablet or mobile of client accessing your Website.

Required Libraries

You need to download

  1. JDK 6
  2. Eclipse 3.7
  3. Spring

Following jar must be in classpath

  1. commons-logging-1.1.1.jar
  2. spring-aop-4.1.2.RELEASE.jar
  3. spring-beans-4.1.2.RELEASE.jar
  4. spring-context-4.1.2.RELEASE.jar
  5. spring-context-support-4.1.2.RELEASE.jar
  6. spring-core-4.1.2.RELEASE.jar
  7. spring-expression-4.1.2.RELEASE.jar
  8. spring-mobile-device-1.0.1.RELEASE.jar
  9. spring-web-4.1.2.RELEASE.jar
  10. spring-webmvc-4.1.2.RELEASE.jar

Project Structure

Detect Device Type Using Spring

Detecting Device Type Of Web Application Using Spring

package com.device;

import org.springframework.mobile.device.Device;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DeviceDetectionController {

   
@RequestMapping("/detect-device")
   
public @ResponseBody String detectDevice(Device device) {
       
String deviceType = "unknown";
       
if (device.isNormal()) {
           
deviceType = "normal";
       
} else if (device.isMobile()) {
           
deviceType = "mobile";
       
} else if (device.isTablet()) {
           
deviceType = "tablet";
       
}
       
return "Hello " + deviceType + " browser!";
   
}

}
Output

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

Detect IPAD Device Type Using Spring Detect IPhone Device Type Using Spring Detect Tablet Type Using Spring











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