File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Browser Detection with JSP

 
Heitor Bembom
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a project and I need to be able to detect the browser and platform on the server side so I can add it to the JSP session.
I have already tried:
System.getProperty("browser");
and
System.getProperty("browser.vendor");
THe returned value was null for both. I also tried:
request.getHeader("User-Agent");
and I got this:
on Netscape 4.7:
Mozilla/4.7 [en] (Win98; I)
and on Explorer 5.0:
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
I can do a lot of parsing with these strings and use it for browser and platform detection but I am wondering if thre is an easier way, like the JavaScript's "navigator.appName" statement.
Thank You,
Heitor Bembom
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The browser (JavaScript) property is identical to the one you get with the servlet's request object's method. They both interrogate the browser in the same way.
In my page http://alicea.org/detect.html you can see JavaScript in action in this respect.
 
Heitor Bembom
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Tony,
I visited your page. I am aware of how to detect browser and platform using JavaScript. I am just wondering if there is a way to do the same with Java. Another question: How can you get the screen resolution. What method do you use for that?
Thank You,
Heitor Bembom

 
Todd M Bush
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a way we are doing it. Granted, we just care if it's IE or not.
String browser = request.getHeader("User-Agent");
if(browser.indexOf("MSIE") > 0) {
// do ie stuff
}
else {
// do Netscape or other browser stuff
}
Hope it helps
 
Bhuvana
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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;

}
All the Best,
Bhuvana
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic