Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Browser Sniffing

Status
Not open for further replies.

jsulman

Programmer
Jun 14, 2001
85
US
Is there any way I can sniff for the browser type from a servlet?

Thanks in advance

Jeff Sulman
 
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);



_____________________________________________________________________
onpnt2.gif

Hakuna matata!!
 
getting this info on the client is a bit more simplicit and resource friendly task also.
eg:
<SCRIPT LANGUAGE = &quot;JavaScript&quot;>
var version = 0;
if (navigator.userAgent.indexOf(&quot;Mozilla/3.0&quot;) != -1)version = 3;
else if
(navigator.userAgent.indexOf(&quot;MSIE&quot;) != -1) version = 1;
else if (navigator.userAgent.indexOf(&quot;Mozilla/2.0&quot;) != -1) version = 2;
else version = 0;
function browser_type()
{
if (version == 3) return &quot;Netscape Version 3.0&quot;;
if (version == 2) return &quot;Netscape Version 2.x&quot;;
if (version == 1) return &quot;Microsoft Internet Explorer 3.0&quot;;
if (version == 0) return &quot;an off-the-wall JavaScript browser&quot;;
}
</SCRIPT>


_____________________________________________________________________
onpnt2.gif

Hakuna matata!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top