Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Device Type in a web application

We have a Java based application where in we want to detect the device type(mobile or desktop) for the device that is sending the request.

How is it possible?

like image 643
Rishabh Ohri Avatar asked Dec 15 '11 04:12

Rishabh Ohri


People also ask

How do I find a device on a website?

You can use JavaScript window. matchMedia() method to detect a mobile device based on the CSS media query. You may also use navigator. userAgentData.

What is device detection?

Device Detection is the process of capturing accurate real-time intelligence about the devices being used to access online information, including web and native apps. This Device Intelligence is used to identify where online content is performing best on any device, regardless of screen size.

What is the best way to detect a mobile device in JavaScript?

To detect if the user is using a mobile device in JavaScript, we can use the userAgent property. This property is part of the navigator object and sent by the browser in HTTP headers. It contains information about the name, version, and platform of the browser.


1 Answers

You'll have to read the User-Agent header from the request and decide on that.

In vanilla servlet apps, a crude way of doing it is:

public void doGet(HttpServletRequest request,                 HttpServletResponse response) throws ServletException, IOException {   if(request.getHeader("User-Agent").contains("Mobi")) {     //you're in mobile land   } else {     //nope, this is probably a desktop   } } 
like image 192
leonardoborges Avatar answered Oct 08 '22 15:10

leonardoborges