How to display browser name and version with JSF 2.0?

I just need to display the name of the browser and its version on <h:outputText/>the user's home page. Can we achieve this through JSF 2.0?


Mojarra 2.0.4 - Primary Surfaces 2.2.1-glassfish v3

+5
source share
2 answers

As far as I know, there are no JSF components that do this with a single tag or something else. The simplest thing you can do is simply display the original HTTP header User-Agent.

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

This is just a large and ugly line that does not always decrypt for everyone.

API-, HTTP User-Agent , ​​ / / , useragentstring.com.

User-Agent API JSF bean .

+9

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";
}

:

<h:outputText value="Browser: #{yourBean.browserName}" />
+12

All Articles