Solved

Browser Identification in JSP

Posted on 2001-07-30
5
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
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 …

626 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