Solved

Browser Identification in JSP

Posted on 2001-07-30
5
333 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

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

929 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

12 Experts available now in Live!

Get 1:1 Help Now