Solved

Browser Identification in JSP

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help in good tutorials for PHP, HTML and CSS 6 41
Troubles viewing HTML in Outlook 2010 client 3 30
html border input line 7 16
..ignore the Question 1 8
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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 …

828 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