Friday, 29 April 2016

How to get the device type (User Agent) in AEM

How to get the device type (User Agent) in AEM

Sometimes when developing multiple platform websites we need to know the device type the user is logged in from.

This can be easily achieved by using the HttpServletRequest from the component JSP.
This can be done using request object,the HttpServletRequest implicit object in JSP


The HttpServletRequest.getHeader() methods returns the headers of the request.
 java.lang.StringgetHeader(java.lang.String name)
Returns the value of the specified request header as a String.

"user-agent" is the property which contains the type of device from which the request is made from.

From a component JSP this can be done using the following code.

<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" %>

    String deviceType=request.getHeader("user-agent");

OUTPUT for various devices:

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36

Windows/IE v11.0
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

Android/Nexus 5X:
Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36

Android/Galaxy S5:
Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36

IOS/Iphone 6:
Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1

No comments:

Post a Comment

Coder Magnet
CoderMagnet is full of resources from our daily development activities. It has solutions for common problematic scenarios in technologies like Java 8, AEM, JCR and also occasionally gives you tips on Blogger as well.