Overview
In this article we will build a browser detection JavaBean which as the name
suggests will try to detect your browser and display a useful message. You can use
this bean as such or improve it to your liking. It is a very simple and light weight bean
and should not take much memory on your server. You can use it on your site to detect
user browser and render appropriate HTML to the user. Since different browsers don't
support all the HTML and CSS features ( Netscape 4 being the main culprit ), this
JavaBean can be of lot of help to you.
This bean though very simple does require basic knowledge of JSP pages and JavaBeans.
I will assume here that you have been following my earlier articles. If you are
uncomfortable with JavaBeans then I suggest you at least read
What are JavaBeans ? and
Calling a JavaBean from JSP page articles.
DetectBrowser JavaBean
Create a new DetectBrowser.java file and place it in the /WEB-INF/classes/com/stardeveloper/bean/test/
folder so that the complete path is :
/WEB-INF/classes/com/stardeveloper/bean/test/DetectBrowser.java
Copy and paste fhe following text into DetectBrowser.java file :
package com.stardeveloper.bean.test;
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
public final class DetectBrowser implements Serializable {
private HttpServletRequest request = null;
private String useragent = null;
private boolean netEnabled = false;
private boolean ie = false;
private boolean ns6 = false;
private boolean ns4 = false;
public void setRequest(HttpServletRequest req) {
request = req;
useragent = request.getHeader("User-Agent");
String user = useragent.toLowerCase();
if(user.indexOf("msie") != -1) {
ie = true;
} else if(user.indexOf("netscape6") != -1) {
ns6 = true;
} else if(user.indexOf("mozilla") != -1) {
ns4 = true;
}
if(user.indexOf(".net clr") != -1)
netEnabled = true;
}
public String getUseragent() {
return useragent;
}
public boolean isNetEnabled() {
return netEnabled;
}
public boolean isIE() {
return ie;
}
public boolean isNS6() {
return ns6;
}
public boolean isNS4() {
return ns4;
}
}
Compile this DetectBrowser.java file into DetectBrowser.class file.
Explanation
Our first line is the package statement.
package com.stardeveloper.bean.test;
Next we import the two classes we are going to use in our bean.
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
Next we start our DetectBrowser class. Notice the use of 'final' keyword. I used it
because a final class is faster than a non-final class. Secondly notice that our
class implements java.io.Serializable interface. It is a must for JavaBeans.
public final class DetectBrowser implements Serializable {
We then declare the six class level variables we are going to use in our class. Their
names are pretty suggestive of their use.
private HttpServletRequest request = null;
private String useragent = null;
private boolean netEnabled = false;
private boolean ie = false;
private boolean ns6 = false;
private boolean ns4 = false;
Then we start our main method; setRequest(). This method receives the HttpServletRequest
reference from the JSP page and stores it internally in a private variable.
public void setRequest(HttpServletRequest req) {
Store the HttpServletRequest reference internally.
request = req;
Get the User-Agent header from the HttpServletRequest sent by the user browser. Notice
that every browser sends User-Agent header to the requested page. We retrieve that header
using HttpServletRequest.getHeader(headerName) method and store in the private
variable useragent.
useragent = request.getHeader("User-Agent");
We make a local copy of this useragent String. The only thing different between this
local copy and the class variable is that this local copy is all in lower case. We do
this so that later we can easily search for a given sub-string using all lower case
characters e.g. msie.
String user = useragent.toLowerCase();
Since user is of type String, we can use String.indexOf() method to search for a
sub-string in the user String. If a sub-string is not found then String.indexOf() returns
-1. We check for this -1 int variable. If user ( the User-Agent String ) variable
contains a given sub-string ( e.g. msie, netscape6, mozilla ) then a positive integer
will be returned. Depending on the sub-string found, we set the specific internal class
variable to true.
I used following sub-strings to look for following browser :
- msie - Microsoft Internet Explorer
- netscape6 - Netscape 6
- mozilla - Netscape 4
This was because on my system these browsers returned following User-Agent headers :
- Microsoft Internet Explorer -
Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; .NET CLR 1.0.2914)
- Netscape 6.0 Browser -
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; m18) Gecko/20001108 Netscape6/6.0
- Netscape 4.7 Browser -
Mozilla/4.7 [en] (WinNT; I)
if(user.indexOf("msie") != -1) {
ie = true;
} else if(user.indexOf("netscape6") != -1) {
ns6 = true;
} else if(user.indexOf("mozilla") != -1) {
ns4 = true;
}
Last step in our setRequest() method is to search for a ".NET CLR" String in the
User-Agent header to see if the user browser is .NET enabled or not. When .NET CLR is
installed on any system, it also looks for Internet Explorer and appends ".NET CLR x.x.xxx"
to the User-Agent header.
if(user.indexOf(".net clr") != -1)
netEnabled = true;
We are now done with the setRequest() method. Let's close it.
}
Then we create five public method to allow the JSP page to get the information we
retrieved and set in out setRequest() method.
public String getUseragent() {
return useragent;
}
public boolean isNetEnabled() {
return netEnabled;
}
public boolean isIE() {
return ie;
}
public boolean isNS6() {
return ns6;
}
public boolean isNS4() {
return ns4;
}
Our DetectBrowser is finished. Let us close it.
}
We are done with DetectBrowser bean. We have created the DetectBrowser.java file,
studied it's code and compiled it. We now need to create a JSP page to test this bean.
This is what we are going to do on the next page.
DetectBrowser.jsp JSP page
Although I have named DetectBrowser.jsp page on the name of the JavaBean DetectBrowser,
you don't have to do it as it is not required to name JSP page according to the JavaBean.
Create a new DetectBrowser.jsp page and place it in the /WEB-APP folder. WEB-APP is
the complete path to your web application e.g. C:\yoursite, then place DetectBrowser.jsp
page in C:\yoursite\DetectBrowser.jsp. Remember, never put JSP pages in /WEB-INF/ folder.
Copy and paste the following code in it :