?
Solved

How to detect browser agent?

Posted on 2001-06-06
8
Medium Priority
?
236 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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