How to get browser information in JSP?

The following jsp will output your ip address and user-agent:

Your user-agent is: <%=request.getHeader("user-agent")%><br/>
Your IP address is: <%=request.getRemoteAddr()%><br/>

To find out what browser and/or OS the user is using, parse the user-agent header.

For example:

<%
String userAgent = request.getHeader("user-agent");
if (userAgent.indexOf("MSIE") > -1) {
  out.println("Your browser is Microsoft Internet Explorer<br/>");
}
%>

For a list of user agents, look here.

 


要判斷Android 很簡單,Agent 裡會有 Android:

目前熱門行動的裝置的User Agent String:

  1. iPad: 有兩個,擇一使用
    (1)

    Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

    (2)

    Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

     

  2. iPod
    Mozilla/5.0 (iPod; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16

     

  3. iPhone
    Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16

     

  4. Android上的瀏覽器
    Android系統的行動裝置幾乎都有“Android”字眼,且多搭配webkit引擎的瀏覽器,所以可用這兩者作為關鍵字

而Apple產品的User Agent String幾乎都包含iPhone字眼。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *