2010-04-25 100 views
10

Java Servlets - 如何檢測用戶是否來自移動設備?Java Servlets - 如何檢測用戶是否來自移動設備?

我正在使用TinyMCE javascript編輯器,它不能在iphone上工作。如何檢測用戶是否來自移動設備?

+2

除了 - 見的移動裝置的這個有趣的定義:http://www.zytrax.com/tech/web/mobile_ids.html#mobile – 2010-04-25 06:41:47

回答

4

我使用TinyMCE的JavaScript編輯器

既然你想改變這取決於客戶端上的客戶端的行爲,最好是在客戶端來處理這個,而不是服務器端。

在CSS世界中,您可以掛鉤media type以根據使用的媒體應用樣式。大多數使用的媒體類型是屏幕(通常是PC),手持設備(通常是手機)和打印(用於打印頁面)。

你可以利用它來隱藏在你的CSS編輯器僅由以下規則:

@media handheld { 
    #elementIdContainingEditor { display: none; } 
} 

你甚至可以根據使用的媒體上指定單獨的樣式表。

<link rel="stylesheet" href="default.css" media="screen"> 
<link rel="stylesheet" href="mobile.css" media="handheld"> 

如果問題其實更多,JavaScript是特定的客戶端上禁用它不工作,那麼你最好有執行特定CSS當JS被禁用是:

<noscript><style>#elementIdContainingEditor { display: none; }</style></noscript> 

或者反過來想,最初隱藏它,然後顯示它已啓用JS時:

<script>document.getElementById('elementIdContainingEditor').style.display = 'block';</script> 

這比在服務器端嗅劑更可靠。

0

唯一不同的就是用戶代理。查找您想要檢測的瀏覽器的用戶代理。 (不知道你爲什麼會在意)

你也可以添加一些JavaScript來在瀏覽器上運行一些東西?

8

使用request.getHeader("User-Agent")Here是移動瀏覽器及其相應用戶代理的列表。

0

在HTTP請求標頭中使用User-Agent。

request.getHeader("User-Agent") 
23

我已經使用了UAgentInfo.java類,你可以在這裏下載(http://code.google.com/p/mobileesp/source/browse/Java/UAgentInfo.java):

private boolean isRequestComingFromAMobileDevice(final HttpServletRequest request){ 

    // http://www.hand-interactive.com/m/resources/detect-mobile-java.htm 
    final String userAgent = request.getHeader("User-Agent"); 
    final String httpAccept = request.getHeader("Accept"); 

    final UAgentInfo detector = new UAgentInfo(userAgent, httpAccept); 

    return detector.detectMobileQuick(); 
} 

UAgentInfo類有一堆的方法來檢測特定器件。例如,只需替換detect.detectMobileQuick(),例如,detect.detectIphoneOrIpod(),detection.detectKindle()等。

更新:如果您使用Spring,則可能需要使用其本機實現。下面是一個例子:http://spring.io/guides/gs/device-detection/

+0

這是:http:/ /search.maven.org/#search|ga|1|com.github.autograder – PaulG 2014-04-13 06:26:16

0
public void doGet(HttpServletRequest request, 
       HttpServletResponse response) throws ServletException, IOException { 
    if(request.getHeader("User-Agent").indexOf("Mobi") != -1) { 

    } else { 

    } 
}