Get IE Browser Version Using Java
The code below shows you the way to get the version of IE browser using Java. To do this, we simply look at User-Agent data (request.getHeader("User-Agent")
) sent by the browser to determine whether the visitors are using IE browser.
IEBrowser.java
package com.bytenota;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.servlet.http.HttpServletRequest;
public class IEBrowser {
public static Integer getIEVersion(HttpServletRequest request) {
String userAgent = request.getHeader("User-Agent");
try {
// detect IE below 11
if (userAgent.contains("MSIE")) {
String[] parts = userAgent.split("MSIE");
int version = NumberFormat.getInstance().parse(parts[1]).intValue();
return version;
}
// detect IE 11
if (userAgent.contains("Trident/")) {
String[] parts = userAgent.split("rv:");
int version = NumberFormat.getInstance().parse(parts[1]).intValue();
return version;
}
} catch (ParseException ex) {
}
// not found
return null;
}
}
Use the method:
index.jsp
<%@page import="com.bytenota.IEBrowser"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Return the version of IE browser using Java</title>
</head>
<body>
<%
// Mozilla/4.0 (compatible; MSIE 5.50; Windows 98; SiteKiosk 4.8)
out.write(IEBrowser.getIEVersion(request).toString()); // IE version: 5
// Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)
out.write(IEBrowser.getIEVersion(request).toString()); // IE version: 6
// Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)
out.write(IEBrowser.getIEVersion(request).toString()); // IE version: 7
// Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)
out.write(IEBrowser.getIEVersion(request).toString()); // IE version: 8
// Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; Media Center PC 6.0; InfoPath.3; MS-RTC LM 8; Zune 4.7)
out.write(IEBrowser.getIEVersion(request).toString()); // IE version: 9
// Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0
out.write(IEBrowser.getIEVersion(request).toString()); // IE version: 10
// Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
out.write(IEBrowser.getIEVersion(request).toString()); // IE version: 11
%>
</body>
</html>