Solved

Browser Identification in JSP

Posted on 2001-07-30
5
337 Views
Last Modified: 2008-03-03
How can I identify Different Browsers using JSP code
i.e
if Internet Explorer then
do this
else if Netscape Navigator 4 then
do this
else if Netscape Navigator 6 then

do this

....
0
Comment
Question by:gondesiravi
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:psogaa
ID: 6333299
Usually, when you just want to check whether it's IE or NN, you can just ask to a non-existing object in either:

if( document.all ) // = if IE


If you want further info, and want to distinguish between browser versions, try some of the following :)


<script language="javascript">
//-- Putting browser info into global variables in the head of a page
//-- gives access to those variables to all scripts within the page.
var browserName = navigator.appName
var browserVersion = navigator.appVersion
var browserVersionNum = parseFloat(browserVersion)
var codeName=navigator.appCodeName
var userAgent=navigator.userAgent
</script>
0
 
LVL 1

Author Comment

by:gondesiravi
ID: 6333330
How can I refer to the global variables (browsername, browserversion etc.,) in JSP
0
 
LVL 1

Author Comment

by:gondesiravi
ID: 6333333
How can I refer to the global variables (browsername, browserversion etc.,) in JSP
0
 
LVL 2

Expert Comment

by:psogaa
ID: 6333368
Bah sorry...it's still morning here so still a bit slow :)
I missed the fact that you were using jsp ( thought it was just javascript ).

I'm not very familiar with JSP, but you can try and check the value of  request.getHeader("User-Agent")  in each browser and see if you can use that to distinguish between them.

0
 
LVL 19

Accepted Solution

by:
cheekycj earned 10 total points
ID: 6334632
how to do it in jsp:
http://www.daysite.net/programming/jsp.htm

simple:
String browser = request.getHeader("User-Agent");
if(browser.indexOf("MSIE") > 0) {
// do ie stuff
}
else {
// do Netscape or other browser stuff
}


Here i have given the method which accepts the value which you retrieved from getHeader("USER-AGENT") and return only the Browser type whether "NS version" or "IE version".
I think you can use this method in your program to the Browser value.

private String getBrowserType(String currValue){
String browser = new String("");
String version = new String("");
if(currValue != null ){
if((currValue.indexOf("MSIE") == -1) && (currValue.indexOf("msie") == -1)){
browser = "NS";
int verPos = currValue.indexOf("/");
if(verPos != -1)
version = currValue.substring(verPos+1,verPos + 5);
}
else{
browser = "IE";
String tempStr = currValue.substring(currValue.indexOf("MSIE"),currValue.length());
version = tempStr.substring(4,tempStr.indexOf(";"));

}

}
System.out.println(" now browser type is " + browser +" " + version);

return browser + " " + version;

}
 
<% String browser = getBrowserType(request.getHeader("User-Agent")); %>

Now you can use browser var as you please.

CJ
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
form button worked now it doesnt anymore 9 53
Set differnt row widths in a table. 3 21
Width of Page is Affecting Side Menu 6 24
Javascript_Set_dropdown_value 7 32
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

786 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