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?
You can use JavaScript window. matchMedia() method to detect a mobile device based on the CSS media query. You may also use navigator. userAgentData.
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.
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.
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With