How to get browser details in AEM Servlet? | Community
Skip to main content
Anderson_Hamer
Level 4
Sep 3, 2021 9:12 PM UTC
Solved

How to get browser details in AEM Servlet?

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"

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
VKumar2Accepted solution
Level 2
Sep 4, 2021 10:01 PM UTC

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,

9 replies

New Participant
Feb 17, 2022 3:58 AM UTC

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.

Translate
Rohit_Utreja
New Participant
Sep 6, 2021 10:01 AM UTC

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/18030465

 

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);
Translate
BrianKasingli
New Participant
Sep 5, 2021 5:06 AM UTC

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"
Translate
VKumar2Accepted solution
Level 2
Sep 4, 2021 10:01 PM UTC

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,

Translate
RitendraS11
Level 4
Sep 4, 2021 10:15 AM UTC

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!

Translate
kautuk_sahni
Employee
Sep 6, 2021 11:47 AM UTC

RitendraS11  Good to have you in AEM community. Keep assisting other. 

Translate
Kautuk Sahni
rampai
Level 1
Sep 4, 2021 1:56 AM UTC

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?

https://www.google.com/amp/s/www.geeksforgeeks.org/how-to-detect-the-user-browser-safari-chrome-ie-firefox-and-opera-using-javascript/amp/

 

Thanks, 

Ram

 

Translate
sachinn20956098
Level 2
Sep 4, 2021 1:54 AM UTC

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. 

Translate
kautuk_sahni
Employee
Sep 6, 2021 11:46 AM UTC

@sachinn20956098 Good to have you in AEM community. Keep assisting other. 

Translate
Kautuk Sahni