Solved

How to detect browser agent?

Posted on 2001-06-06
8
202 Views
Last Modified: 2010-08-05
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>
0
Comment
Question by:sdesar
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6162061
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
0
 

Author Comment

by:sdesar
ID: 6162097
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!
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6163122
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>
0
 

Author Comment

by:sdesar
ID: 6164841
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>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Accepted Solution

by:
TimYates earned 10 total points
ID: 6166975
Try putting

<H1><%= request.getHeader(propName); %></H1>

inside the body of your jsp...

What does that show for Netscape and for IE?

I am guessing that neither of them has the word "Netscape" at the start...

You could also try changing:

if ( propName.indexOf("Netscape") == 0 ) {

to

if ( propName.indexOf("Netscape") > -1 ) {

As that sould fix it (so long as Netscape actually does have the word "Netscape" somewhere in it's User-Agent string...

Good luck!!

Tim.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6166978
PS:  Phew what a lot of effort for 10 points ;-) ;-)

Hee hee!!  The cheek :-)

Good luck again!!!
0
 

Author Comment

by:sdesar
ID: 6168895
I had to check for the propValue!
 and > -1 worked!
Thanks a million!
0
 

Author Comment

by:sdesar
ID: 6207737
u6kfhkjytufnhrykjyiiummnytmukyutmkil8hjfnjkykuilutlnfhjmyukuitktukiykhfnuykt7ykui,jgfrey
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 22
for i loop in grovy 1 45
why cannot we forward request once the response is committed 2 33
servlet example issue 6 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now