Pin Me

How to Detect Browser in JSP

written by: theinkandpen (Robert Mullon)•edited by: Robin L.•updated: 10/27/2008

An overview on how to use Java Server Pages to detect the users browser. Included in the overview is a sample script.

  • slide 1 of 2


    A common problem with static content is that the web page’s layout and display differs between browsers, and often renders the page inaccessible or cluttered. With dynamic content, such difficulties can be overcome; we will particularly be focussing on how the Java Server Pages framework overcomes these difficulties.

    It is possible to allow for content to be cross-compatible with different types of browsers by employing a ‘Javabean’, or a reusable object which can be called upon for a particular function. This is a class instance written in Java which conforms to a particular convention. In this case, we will be looking at writing the ‘DetectBrowser’ Bean, in order to detect the user’s browser which is helpful if you need to retrieve another page version from the server in case of difficulties.

  • slide 2 of 2

    ‘DetectBrowser’ script

    This coding example is courtesy of The site has great Java tutorials which are also helpful for beginners.

    First create a file named and place it in the appropriate directory related to your environment.

    package com.stardeveloper.bean.test;


    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;



    Now you can create a JSP page in order to test the above. Name your page DetectBrowser.jsp and use the following code:



    <title>DetectBrowser Bean</title>


    p, ul, li { font-family:Trebuchet MS,Verdana; font-size:12pt; }




    <p align="center"><u><b>DetectBrowser Bean</b></u></p>

    <jsp:useBean id="db" class="com.stardeveloper.bean.test.DetectBrowser"


    <jsp:setProperty name="db" property="request" value="<%= request %>" />


    <p>User-Agent : <b>

    <jsp:getProperty name="db" property="useragent" />


    <p>.NET Enabled : <b>

    <jsp:getProperty name="db" property="netEnabled" />



    <li>IE : <b><%= db.isIE() %></b></li>

    <li>NS6 : <b><%= db.isNS6() %></b></li>

    <li>NS4 : <b><%= db.isNS4() %></b></li>


    <p style="padding-left:30;"><% if(db.isIE()) { %>

    Internet Explorer - By far the best browser you can get.

    <% } else if(db.isNS6()) { %>

    Netscape 6 - If you cannot run Internet Explorer, use Netscape 6.

    <% } else if(db.isNS4()) { %>

    Netscape 4 - You have got to upgrade your browser!

    <% } else { %>

    Other - Your browser could not be detected by DetectBrowser Bean.

    Please report to

    <a href=""></a>

    and tell him this message that you got along with the User-Agent

    string above.

    <% } %></p>