Link to home
Start Free TrialLog in
Avatar of bcops
bcops

asked on

Browser Detection using Java

Hi,
I am very very new to Java programming and was hoping someone could help me. I am sure what i am doing is really simple but at the moment it is doing my head in.

I need to be able to detect which browser and version is being used to view my jsp.

I have tried a couple of browser sniff examples using JSPs and Java Beans but they did not work.(have no idea why)

I am limited to using TOMCAT and J2SE and cannot use J2EE. Would prefer examples using Servlets or just JSPs, so any working examples or code snipets would be great.

Thanks for your help
Avatar of 91mustang
91mustang

<% // get User Agent header from request
String usrAgent = request.getHeader("User-Agent").toLowerCase();
if (usrAgent == null) usrAgent = "";
// platform
boolean is_mac68k = ((usrAgent.indexOf("68k") > -1) || (usrAgent.indexOf("68000") > -1));
boolean is_macppc = ((usrAgent.indexOf("ppc") > -1) || (usrAgent.indexOf("powerpc") > -1));
boolean is_mac = ((usrAgent.indexOf("mac") > -1) || is_mac68k || is_macppc);
// browser
boolean is_msie = (usrAgent.indexOf("msie") > -1);
double version = 0.0;
// get MSIE version
if (is_msie) {
  try {
    String tempStr = usrAgent.substring(usrAgent.indexOf("msie"),usrAgent.length());
    version = Double.parseDouble(tempStr.substring(4,tempStr.indexOf(";")));
  } catch (NumberFormatException nfe) {
       version = 0.0;
  }
       catch (StringIndexOutOfBoundsException siobe) {
     version = 0.0;
  }
}
boolean is_msie4up  = (is_msie && (version >= 4.00));
// decide whether to use JavaScript or Not
Boolean useJS = new Boolean(!is_mac && is_msie4up);
request.setAttribute("useJS", useJS);
%>
thats last one gets OS and browser...
ASKER CERTIFIED SOLUTION
Avatar of xjiang007
xjiang007

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bcops

ASKER

xjiang007 you are saying that i can just put that code into a jsp or a servlet?

Would prefer to use a servlet, so how would it work in a servlet?
thanks
S
try this..simple code...

<logic:match header="User-Agent" value="IE">
  <logic:match header="User-Agent" value="6.0">
    <p>Microsoft Internet Explorer 6.0</p>
  </logic:match>
</logic:match>
<logic:match header="User-Agent" value="Opera">
  <p>Opera</p>
</logic:match>
<logic:match header="User-Agent" value="Mozilla">
  <p>Mozilla</p>
</logic:match>

R.K
Avatar of bcops

ASKER

thanks RK but where do i put that code and could you possibly explain how it works? What is that using?
The Jakarta Struts <logic:match> tag can be used to detect the version of the web browser being used to view the page. The tag performs a case-insensitive substring search using the value specified. This enables the User-Agent string in the HTTP header to be queried for the browser version.

The code given below displays “Microsoft Internet Explorer 6.0” if running IE 6.0, or “Opera” or “Mozilla” if running any version of those browsers.

R.K
The requesting browser and all sorts of info can be found in the HTTP request header. Check out this link. It shouls tell you all you need in a much nicer format than we can achieve here.

http://www.stardeveloper.com/articles/display.html?article=2001072201&page=1

Cheers.
Avatar of bcops

ASKER

thanks all!