referenced from:
private String getOs(String userAgent) {
String retVal;
int ix;
String lowerUA = userAgent.toLowerCase();
if (lowerUA.indexOf("win95"

> -1) {
return "Windows 95";
} else if (lowerUA.indexOf("win98"

> -1) {
return "Windows 98";
} else if ((ix = lowerUA.indexOf("windows"

) > -1) {
retVal = userAgent.substring(ix);
int ix2 = lowerUA.indexOf("windows", ix + 7);
if (ix2 > -1) {
retVal = userAgent.substring(ix2);
}
} else if ((ix = lowerUA.indexOf("linux"

) > -1) {
return "Linux";
} else if ((ix = lowerUA.indexOf("x11"

) > -1) {
return "UNIX";
} else if ((ix = lowerUA.indexOf("mac os"

) > -1) {
retVal = userAgent.substring(ix);
} else if ((ix = lowerUA.indexOf("mac"

) > -1) {
return "Mac";
} else {
return "Unknown";
}
// clean up the strings that need cleaning
retVal = retVal.trim();
if ((ix = retVal.indexOf(";"

) > -1) {
retVal = retVal.substring(0, ix);
}
if ((ix = retVal.indexOf("

"

) > -1) {
retVal = retVal.substring(0, ix);
}
return retVal;
}
private String getBrowser(String userAgent)
{
String retVal;
if (userAgent.indexOf("opera"

> -1) {
retVal = "Opera";
} else if (userAgent.indexOf("msie"

> -1) {
retVal = "Internet Explorer";
} else if (userAgent.indexOf("netscape"

> -1) {
retVal = "Netscape";
} else if (userAgent.indexOf("mozilla"

> -1) {
retVal = "Netscape";
} else {
retVal = "Unknown";
}
return retVal;
}
private String getBrowserVersion(String userAgent) {
String retVal;
int ix;
String lowerUA = userAgent.toLowerCase();
if ((ix = lowerUA.indexOf("opera"

) > -1) {
retVal = userAgent.substring(ix + 5);
} else if ((ix = lowerUA.indexOf("msie"

) > -1) {
retVal = userAgent.substring(ix + 4);
} else if ((ix = lowerUA.indexOf("netscape"

) > -1) {
retVal = userAgent.substring(ix + 8);
} else if ((ix = lowerUA.indexOf("mozilla"

) > -1) {
retVal = userAgent.substring(ix + 7);
} else {
retVal = "Unknown";
}
// clean up the strings
retVal = retVal.trim();
if ((ix = retVal.indexOf(" "

) > -1) {
retVal = retVal.substring(0, ix);
}
if ((ix = retVal.indexOf(";"

) > -1) {
retVal = retVal.substring(0, ix);
}
if ((ix = retVal.indexOf("/"

) > -1) {
retVal = retVal.substring(ix + 1);
}
return retVal;
}
private String getCodeType(String userAgent) {
String retVal;
int ix = userAgent.indexOf("/"

;
if (ix == -1) {
int ix2 = userAgent.indexOf(" "

;
if (ix2 != -1) {
retVal = userAgent.substring(0, ix2);
} else {
retVal = "Unknown";
}
} else {
retVal = userAgent.substring(0, ix);
}
return retVal;
}
private String getCodeTypeVersion(String userAgent) {
String retVal;
String ct = getCodeType(userAgent);
if (!ct.equals("Unknown"

) {
retVal = userAgent.substring(userAgent.indexOf(ct) + ct.length() + 1);
int ix = retVal.indexOf(" "

;
if (ix != -1) {
retVal = retVal.substring(0, ix);
}
} else {
retVal = "Unknown";
}
return retVal;
}
in your JSP:
// get header information
String header = req.getHeader("user-agent"

;
// parse out os, browser, and codeType
String os = getOs(header);
String browser = getBrowser(header.toLowerCase());
String browserVersion = getBrowserVersion(header);
String codeType = getCodeType(header);
String codeTypeVersion = getCodeTypeVersion(header);
_____________________________________________________________________
Hakuna matata!!