Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user's browser id using JSF?

Is it possible to get the user's browser id number using JSF? I use JBoss 7 for application server.

like image 563
Peter Penzov Avatar asked Dec 26 '11 16:12

Peter Penzov


2 Answers

The browser's user agent string is available as HTTP request header with the name User-Agent. The request headers are in JSF available by ExternalContext#getRequestHeaderMap():

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

No need to haul the raw Servlet API from under the JSF hoods. Always look at the javadoc of ExternalContext first whenever you need to have access to the HTTP servlet request or response.

Keep in mind that request headers (as everything else in a HTTP request) are fully controllable by the enduser. So never assume the information to be correct and valid. Use it for statistics only. If you need to do feature detection, strongly prefer client side languages like JavaScript and/or CSS if possible. They can do that much more reliably.

like image 190
BalusC Avatar answered Oct 07 '22 18:10

BalusC


You can read user-agent header from request to get the detail about the browser

((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getHeaders();
like image 45
jmj Avatar answered Oct 07 '22 16:10

jmj