Solved

How to detect browser agent?

Posted on 2001-06-06
8
191 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

708 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

14 Experts available now in Live!

Get 1:1 Help Now