Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Browser name and version with JSF 2.0?

Tags:

java

jsf

jsf-2

I just need to display the browser name and its version on a <h:outputText/> in the user's homepage. Can we achieve this via JSF 2.0?


Mojarra 2.0.4 - Primefaces 2.2.1- glassfish v3

like image 260
Selvin Avatar asked Mar 02 '11 05:03

Selvin


2 Answers

Put this method in your bean:

public String getBrowserName() {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String userAgent = externalContext.getRequestHeaderMap().get("User-Agent");

    if(userAgent.contains("MSIE")){ 
        return "Internet Explorer";
    }
    if(userAgent.contains("Firefox")){ 
        return "Firefox";
    }
    if(userAgent.contains("Chrome")){ 
        return "Chrome";
    }
    if(userAgent.contains("Opera")){ 
        return "Opera";
    }
    if(userAgent.contains("Safari")){ 
        return "Safari";
    }
    return "Unknown";
}

And then:

<h:outputText value="Browser: #{yourBean.browserName}" />
like image 93
Andrés Canavesi Avatar answered Oct 26 '22 10:10

Andrés Canavesi


There are as far as I know no JSF components which does that with a single tag or something. The easiest what you can do is just displaying the raw HTTP User-Agent header.

<h:outputText value="#{header['user-agent']}" />

This is only a large and ugly string which is not always decipherable to everyone.

There are however APIs which can convert a HTTP User-Agent header into useable information, such as the exact browser make/version and platform make/version, such as useragentstring.com.

Once converted the User-Agent header into useable parts with help of such an API, you must be able to display the parts of interest in JSF with help of a managed bean the usual way.

like image 39
BalusC Avatar answered Oct 26 '22 09:10

BalusC