I am trying to get browser name in servlet but it is giving me all browser details. Is there any way to get accurate browser name ?
String browserName = req.getHeader("User-Agent");
String secNa = req.getHeader("sec-ch-ua");
LOG.info("browserName !!!!!!!"+browserName);
LOG.info("secNa !!!!!!!"+secNa);
Below are log info
FF browser:-
browserName !!!!!!!Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0
secNa !!!!!!! null
Chrome brwoser
browserName !!!!!!!Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
secNa !!!!!!!"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
Opera brwoser :-
browserName !!!!!!!Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 OPR/78.0.4093.147
secNa !!!!!!!" Not A;Brand";v="99", "Chromium";v="92", "Opera";v="78"
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Anderson_Hamer - There are couple of options which you might try to sort out this one. First way is a lengthy way to parse the string(User Agent) and extract the desired result.
Another way you can include below JS file to your AEM code and pass the agent in Ajax call to the servlet or using hidden variable in HTL.
Sample code would be like below (change to AEM specific)
JS File :: https://github.com/bestiejs/platform.js
<script src="platform.js"></script>
<script>
var os = platform.os;
var browser = platform.name + ' ' + platform.version;
</script>
Alternatively you an include the Java dependency in your AEM pom.xml file and get the exact browser name:
<dependency>
<groupId>user-agent-utils</groupId>
<artifactId>user-agent-utils</artifactId>
<version>1.2.3</version>
</dependency>
String userAgent = req.getHeader("user-agent");
UserAgent ua = UserAgent.parseUserAgentString(userAgent);
String browserName = ua.getBrowser().toString();
Thanks,
You will have to parse the User-Agent Header to identify the browser name. You can chose to use a java java library instead of writing this block of logic yourself since you need to actively maintain it.
@sachinn20956098 Good to have you in AEM community. Keep assisting other.
Hi @Anderson_Hamer,
Can't you check this using javascript instead?
Or you can try to apply the below checks using String API in Java.
Could you please check if this helps?
Thanks,
Ram
In JAVA, you need to parse the user-agent string. It can be done by writing a parser of your own, which will lead to maintain it as well. The best recommended way would be to use a dependency as -
<dependency> <groupId>com.blueconic</groupId> <artifactId>browscap-java</artifactId> <version>1.3.6</version> </dependency>
You can check more details from here - https://github.com/blueconic/browscap-java
Hope, this might help!
RitendraS11 Good to have you in AEM community. Keep assisting other.
Hi @Anderson_Hamer - There are couple of options which you might try to sort out this one. First way is a lengthy way to parse the string(User Agent) and extract the desired result.
Another way you can include below JS file to your AEM code and pass the agent in Ajax call to the servlet or using hidden variable in HTL.
Sample code would be like below (change to AEM specific)
JS File :: https://github.com/bestiejs/platform.js
<script src="platform.js"></script>
<script>
var os = platform.os;
var browser = platform.name + ' ' + platform.version;
</script>
Alternatively you an include the Java dependency in your AEM pom.xml file and get the exact browser name:
<dependency>
<groupId>user-agent-utils</groupId>
<artifactId>user-agent-utils</artifactId>
<version>1.2.3</version>
</dependency>
String userAgent = req.getHeader("user-agent");
UserAgent ua = UserAgent.parseUserAgentString(userAgent);
String browserName = ua.getBrowser().toString();
Thanks,
You can try this library, https://github.com/ua-parser/uap-java
import ua_parser.Parser; import ua_parser.Client; ... String uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3"; Parser uaParser = new Parser(); Client c = uaParser.parse(uaString); System.out.println(c.userAgent.family); // => "Mobile Safari" System.out.println(c.userAgent.major); // => "5" System.out.println(c.userAgent.minor); // => "1" System.out.println(c.os.family); // => "iOS" System.out.println(c.os.major); // => "5" System.out.println(c.os.minor); // => "1" System.out.println(c.device.family); // => "iPhone"
Hi @Anderson_Hamer ,
Please refer brloe thread to extract browser information in JAVA code.
https://stackoverflow.com/questions/1326928/how-can-i-get-client-information-such-as-os-and-browser/...
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent")); Browser browser = userAgent.getBrowser(); String browserName = browser.getName(); //or // String browserName = browser.getGroup().getName(); Version browserVersion = userAgent.getBrowserVersion(); System.out.println("The user is using browser " + browserName + " - version " + browserVersion);
Is this really the accepted solution? Parse it yourself or import a third party dependency?
Surely AEM itself must know what sort of browser a request is coming through? There must be some property or other hidden in the Uberjar surely. I won't believe something as fundamental as this isn't in the system somewhere.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies