Solved

How to detect browser agent?

Posted on 2001-06-06
8
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html Table Looping (part 2) 5 35
Java class and jar 3 43
junit example issue 2 28
Java array 10 65
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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