How to Detect the User's Web Browser
Browsers usually identify themselves using the user-agent header. For example, Firefox 3.6 sends this value (split into two lines because it is so long):
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2)
Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)While Internet Explorer 8.0 sends this:
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1;
.NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729)Both strings start with "Mozilla" to claim compatibility with the old Netscape Navigator, but also include the operating system ("Windows NT 6.0" = Windows Vista), browser's internal rendering engine ("Gecko/20100115" and "Trident/4.0") as well as the browser itself ("Firefox/3.6" and "MSIE 8.0"). There is no definite way of identifying the browser, as there is no exact format for the user agent string. Another problem is that browsers try to improve compatibility by including the names of similar browsers into their user agent. Some obscure browsers even copy popular browser's user agent string (usually Internet Explorer's) to prevent sites from rejecting them.
When you really need to identify the exact browser version, use a library like user-agent-utils. Those libraries usually come with large collections of known user agents and add some browser-specific forensics.
Otherwise I would suggest to be pragmatic: identify strings in the user agent that look unique for that particular browser, and check whether the user agent strings contains them. This simple example checks for Internet Explorers and Firefoxes:
String ua = request.getHeader("user-agent");
if (ua.contains("Firefox"))
log("User seems to use Firefox");
else if (ua.contains("MSIE"))
log("User seems to use Internet Explorer");
else
log("User seems to use unknown browser.");

