Solved

Browser Identification in JSP

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Search box won't work. 22 25
Image not there 4 35
Fixing my modal from bootstrap 18 16
Using Classic ASP inside HTML pages 2 11
Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
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.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

746 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

13 Experts available now in Live!

Get 1:1 Help Now