BrowserChromeExample CodeHow-ToTipsWeb

How To Deal With Different Browsers In Java – Playing With User-Agent

2 Mins read

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.

Why Support All Browsers

There are dozens of browsers from various sources. There is no single browser that is used by entire world. A browser comparison can show you that many different browsers are used on different platforms.

Google chrome is a popular browser however there are many people who prefer Firefox over chrome. Majority of windows users still use Internet explorer.

When you are making a web site then your customers can be using any browser. This requires you to support all possible browsers. Most people stick to supporting at least these major browsers.

  • Google Chrome
  • Firefox 
  • Safari
  • Internet Explorer

How To Identify Different Browsers

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.

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

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/ Safari/525.19

Mozilla Firefox user-agent should look something like this:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 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

Hope you find this article useful. Please share your comments with us.

Related posts

How to Create a Marketing Strategy for Web Hosting Companies? 6 Expert Advice

4 Mins read
To increase a viable customer base for your web hosting business, you must create a marketing strategy. Web hosting companies with ineffective…

A Quick Introduction To Web Accessibility

4 Mins read
Accessibility is a human right. In most countries, it is a law. Thus, most physical stores and services are legally required to…
FeaturedWebWeb Design

The Most Important Web Design Trends and Standards for 2022

3 Mins read
Considering the fact that web design determines a website’s credibility for 94% of internet visitors, being familiar with the newest contemporary website…
Power your team with InHype

Add some text to explain benefits of subscripton on your services.

Leave a Reply

Your email address will not be published. Required fields are marked *