Link to home
Start Free TrialLog in
Avatar of sdesar
sdesar

asked on

How to detect browser agent?

I am using JSP and I added the following code to the JSP to determine the broswer type.  If its IE then display one CSS else Netscape and display another CSS.

Its not working! Can some one please help?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>

<%
String propName, propValue;
boolean isIE, isNetscape;
propName = "User-Agent";
propValue = request.getHeader(propName);

if ( propName.indexOf("Netscape") == 0 ) {
  isNetscape = true;
  isIE = false;
}
%>
<link rel="stylesheet" type="text/css" href="unix.css">
<%
else {
isIE = true;
isNetscape = false;
}
%>
<link rel="stylesheet" type="text/css" href="win_ie.css">

</head>

<title> TEST </title>
<body bgcolor=white>
Test to check broswer version
</body>
</html>
Avatar of Igor Bazarny
Igor Bazarny
Flag of Switzerland image

Hi,

Why your links to stylesheet are out of if branches? As far as I understand JSP, this should lead to uncompilable java code:

<%
[declarations skipped]
if ( propName.indexOf("Netscape") == 0 ) {
   isNetscape = true;
   isIE = false;
}
%>
<-- Here you effectively end if because next statement will
translate into output to responce stream -->
<link rel="stylesheet" type="text/css" href="unix.css">
<%
else {
// Here yor should get 'else without if' error message
  isIE = true;
  isNetscape = false;
}
%>
<-- and even if I'm wrong, following link will be in responce in any case.
What is effect of 2 stylesheets? -->
<link rel="stylesheet" type="text/css" href="win_ie.css">

Regards,
Igor Bazarny
Brainbench MVP for Java 1
www.brainbench.com
Avatar of sdesar
sdesar

ASKER

I am fairly new to JSPs.
i have 2 style-sheets. One for IE and one for Netscape.
How should I modify the above code then to
detect what browser the user is using and display the style-sheet accordingly?

I think I am missing an include... where does request.getheader come from?  which library?

help Please!
Avatar of TimYates
Move the links inside the if statement braces:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>

<%
String propName, propValue;
boolean isIE, isNetscape;
propName = "User-Agent";
propValue = request.getHeader(propName);

if ( propName.indexOf("Netscape") == 0 ) {
 isNetscape = true;
 isIE = false;
%>
<link rel="stylesheet" type="text/css" href="unix.css">
<%
}
else
{
isIE = true;
isNetscape = false;
%>
<link rel="stylesheet" type="text/css" href="win_ie.css">
<%
}
%>

</head>

<title> TEST </title>
<body bgcolor=white>
Test to check broswer version
</body>
</html>
Avatar of sdesar

ASKER

Here's my code :- It seems to be picking the else part of the CSS for both IE and Netscape.  It seems the code is not being recognised.  When I vire the code in the browser IE oe Netscape the following appears-
<link rel="stylesheet" type="text/css" href="/gts/jsp/win_ie.css">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>

<%
String propName, propValue;
boolean isIE, isNetscape;
propName = "User-Agent";
propValue = request.getHeader(propName);

if ( propName.indexOf("Netscape") == 0 ) {
  isNetscape = true;
  isIE = false;
  out.println ( "<link rel=\"stylesheet\" type=\"text/css\" href=\"/gts/jsp/unix.css\">" );
}
else {
  out.println ( "<link rel=\"stylesheet\" type=\"text/css\" href=\"/gts/jsp/win_ie.css\">" );

}
%>
</head>

<title> TEST </title>
<body bgcolor=white>

<table border=0 cellspacing=0 cellpadding=0 width="170" summary="Left Contextual Navigation Table" >
  <tr>
  <td NOWRAP width="10"></td>
  <td NOWRAP width="10"></td>
  <td NOWRAP width="130"></td>
  <td NOWRAP width="10"></td>
  <td NOWRAP width="10"></td>
  </tr>
<tr>
    <td valign=TOP colspan="2" width="20" bgcolor="#336699"><font size="3"><img src="/gts/jsp/images/tl_navcorner_20.gif" alt="" border=0 height=20 width=20></font></td>
    <th align=CENTER NOWRAP width="130" bgcolor="#336699" class="menu_header" >
          <font color="#FFFFFF" size="3" class="menu_header">submit </font> </th>
    <td valign=TOP colspan="2" width="20" bgcolor="#336699"><font size="3"><img src="/gts/jsp/images/tr_navcorner_20.gif" alt="" border=0 height=20 width=20></font></td>
  </tr>
<tr> </tr>
</table>
<form accept-charset="UNKNOWN" method="post"
        action="/gts/servlet/GServlet/newissue">

<script>
alert (' entered ' );

</script>
     
     <p class="paragraph"> TEST THE PARAGRAPH </p>
   



<br>
<h>Thank You !   </h>


</body>



HELP PLEASE!

</html>
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

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
PS:  Phew what a lot of effort for 10 points ;-) ;-)

Hee hee!!  The cheek :-)

Good luck again!!!
Avatar of sdesar

ASKER

I had to check for the propValue!
 and > -1 worked!
Thanks a million!
Avatar of sdesar

ASKER

u6kfhkjytufnhrykjyiiummnytmukyutmkil8hjfnjkykuilutlnfhjmyukuitktukiykhfnuykt7ykui,jgfrey