How to deal with different Browsers in Java - playing with user-agent

Finding out what Browser type user is using is always required in web based application. Now when there are just too many browsers floating around you can not rely on single browser support.
Many client side frameworks have come to take care of client side needs related to JavaScript and CSS.



In our recent project, we are required to maintain a audit trail of user profile. This should also contain the type of client browser. So here I am trying to come up with a Browser Utility Java class. This Browser Utility class can be used to identify the client browser types and deal with multiple browsers at server side. I know this seems to be a simple code but I didn't find it done well on the web anywhere else so thought of blogging it here. Hope this helps the beginners in this area.

Below is a sample BrowserUtil Java class, which contains a method called getBrowserType(). If you pass the value of user-agent (which can be found from request.getHeader("user-agent")) this method will return you the enumeration of BrowserType. You can use the BrowserType enum to do further decision making.


package browser.util;

public class BrowserUtil {
public static enum BrowserType {
INTERNET_EXPLORER, MOZILA_FIREFOX, SAFARI, NETSCAPE, GOOGLE_CHROME, FLOCK, UNKNOWN
}

/**
* Each browser sends a the user-agent field with different content
* 
* 
* @param userAgent
* @return
*/
public static BrowserType getBrowserType(String userAgent) {

if (userAgent != null) {
if (userAgent.indexOf("MSIE") != -1)
return BrowserType.INTERNET_EXPLORER;
else if (userAgent.indexOf("Netscape") != -1)
return BrowserType.NETSCAPE;
else if (userAgent.indexOf("Chrome") != -1)
return BrowserType.GOOGLE_CHROME;
else if (userAgent.indexOf("Flock") != -1)
return BrowserType.FLOCK;
else if (userAgent.indexOf("Safari") != -1)
return BrowserType.SAFARI;
else if (userAgent.indexOf("Firefox") != -1)
return BrowserType.MOZILA_FIREFOX;

}
return BrowserType.UNKNOWN;
}
}



Below is a sample class, which demonstrates how you can use this method.

package browser.util;

import javax.servlet.http.HttpServletRequest;

import browser.util.BrowserUtil.BrowserType;

public class TestBrowserUtil {

public static void sample(HttpServletRequest request) {

String s = request.getHeader("user-agent");
BrowserType bType = BrowserUtil.getBrowserType(s);
if (bType.equals(BrowserType.INTERNET_EXPLORER)) {
// Do IE stuff here
} else if (bType.equals(BrowserType.GOOGLE_CHROME)) {
// Do Chrome stuff here
} else if (bType.equals(BrowserType.UNKNOWN)) {
// DO generic stuff
}
// and so on....
}
}



In this code snippet I have selected a few known browsers, if you need to do changes related to some other browsers then you need to do two things
1. Add a new enum value for new browser type.
2. Add another "else if" block in the getBrowserType method and return your new enum value.

Here are few sample user-agent values for different browsers. The value may change based on the browser versions and Operating system.
Therefore coding needs to be done only based on the common keywords which are always present in user-agent field. (e.g. MSIE, Chrome, Safari, Firefox)

If you see user-agent for Google Chrome closely you will find "Safari" keyword in it. This is reason I have kept my "if condition" for checking Chrome before Safari so that it doesn't fail.

Google Chrome user-agent should look something like this:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.65 Safari/525.19


Mozilla Firefox user-agent should look something like this:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)


Microsoft Internet Explorer user-agent should look something like this:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

Apple Safari on Macintosh user-agent should look something like this:

Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_6; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1

Apple Safari on Windows user-agent should look something like this:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.28 (KHTML, like Gecko) Version/3.2.2 Safari/525.28.1

Related

Tips 5706602588988239858

Post a Comment Default Comments

  1. why would u need to deal with all kind of browsers. I guess the major browser types cover more then 90% or the clients. how many people use Flock??

    ReplyDelete
  2. Well this was true few years ago when IE was market leader. Now a days there are many people who use different browsers. Firefox is one of the very popular browsers, Flock has good social networking features and is used by many people.

    I don't have the exact numbers but if you have a application where you can log the user agent details then you should be able to figure out the distribution.

    ReplyDelete

Individuals who comment on FromDev at regular basis, will be rewarded in Top Commenter section. (Comments are selectively moderated so please do not spam)

emo-but-icon

item