private void getBrowserData(HttpServletRequest request)
{
String ag = request.getHeader("User-Agent");
ag = ag.toLowerCase();
if (ag.contains("msie")) browser = "IE";
else if (ag.contains("opera")) browser = "Opera";
else if (ag.contains("chrome")) browser = "Chrome";
else if (ag.contains("firefox")) browser = "Firefox";
else if (ag.contains("safari") && ag.contains("version")) browser = "Safari";
}
Here is the output for request.getHeader("User-Agent") for these browsers above.
Firefox: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Chrome: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.4.154.25 Safari/525.19
IE: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; .NET CLR 2.0.50727)
Opera: Opera/9.62 (Windows NT 5.1; U; en-GB) Presto/2.1.1
Safari: Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE) AppleWebKit/525.26.2 (KHTML, like Gecko) Version/3.2 Safari/525.26.13
As you can see, some browsers share certain data. For example Mozilla as the first word. Chrome has Safari in there as well. Its beyond the scope of this post to go into WHY this happens so I just showed how to get around it.
This is exactly what the above method getBrowserData(HttpServletRequest request) does. The last else if for Safari is sure that Chrome is covered and is not the browser in question so it knows its Safari, but in case some OTHER browsers also have Safari in their User-Agent data this will cause problems. For example, Orca browser shows this data:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051214 Firefox/1.5
which is clearly identical to Firefox User-Agent data...