This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Browser Detection with JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Browser Detection with JSP" Watch "Browser Detection with JSP" New topic

Browser Detection with JSP

Heitor Bembom

Joined: Jul 11, 2000
Posts: 10
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:
THe returned value was null for both. I also tried:
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

Joined: Jan 30, 2000
Posts: 3222
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 you can see JavaScript in action in this respect.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Heitor Bembom

Joined: Jul 11, 2000
Posts: 10
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

Joined: Apr 26, 2000
Posts: 22
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

Joined: Aug 21, 2000
Posts: 2
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);
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,
I agree. Here's the link:
subject: Browser Detection with JSP
Similar Threads
Bookmark an URL with encoded values
Mozilla vs Opera
JavaScript with Mozilla
AJAX: request not sent properly
how to know whether the browser is IE or netscape