Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the exact client browser name and version in Spring MVC?

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?

like image 602
hamed Avatar asked Feb 04 '15 13:02

hamed


People also ask

How can you find the browser name of the client?

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.

How do you check which browser is being used in Java?

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.

Where do I find Spring Framework version?

You cann check it in pom. xml file in <org. springframework-version> tag.

Is Spring MVC still used?

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.


2 Answers

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());
like image 190
Master Slave Avatar answered Oct 28 '22 02:10

Master Slave


Useful library for parsing the result of User-Agent Http Header : browscap-java

like image 28
A. Masson Avatar answered Oct 28 '22 00:10

A. Masson