I'm working on a Spring MVC application, and I need to access client browser name and version.
I have an instance of HttpServletRequest
in my action as a parameter and use request.getHeader("User-Agent")
method, but this returned Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
in Internet explorer 9.
I need to exact number and version. Is there any tools for doing that?
Answer: To establish the actual name of the user's Web browser, you can use the navigator. appName and navigator. userAgent properties. The userAgent property is more reliable than appName because, for example, Firefox or Opera may return the string "Netscape" as the value of navigator.
getHeader("user-agent"); // agent will looks like // Mozilla/5.0 (Windows NT 5.1; rv:23.0) Gecko/20100101 Firefox/23.0 // Mozilla/2.0 (compatible; MSIE 6.0; Windows NT 5.2) // Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.
You cann check it in pom. xml file in <org. springframework-version> tag.
You can use Spring MVC if you want to develop a web application with Spring. However, for the development of general Spring applications or beginning to learn Spring, it is recommended that you use Spring Boot as it is production-ready, mitigates the job, and is being quickly adopted.
Acknowledging that the user agent is unsafe. Still, in the lack of other ways, you should parse a user-agent header, which in fact is not as easy, as the number of combinations is overwhelming. Unless you want to roll your own, I would suggest
http://www.bitwalker.eu/software/user-agent-utils
source is available at
https://github.com/HaraldWalker/user-agent-utils
the usage is quite straightforward
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
System.out.println(userAgent.getBrowser().getName() + " " + userAgent.getBrowserVersion());
Useful library for parsing the result of User-Agent
Http Header : browscap-java
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