Getting browser type using a JSP?

Question:
How to get the browser type from a JSP?

Code:
<%
  String browser = "unknown";
  String ua = request.getHeader("User-Agent");
  if (ua.contains("MSIE")){
    browser = "IE";
  } else if (ua.contains("Firefox")){
    browser = "Firefox";
  } else if (ua.contains("Safari")){
    browser = "Safari";
  } else if (ua.contains("Chrome")){
    browser = "Chrome";
  }
 
  out.print("Browser: " + browser);
%>
 

Output:
Browser: Firefox