Java Servlets - How do I detect if a user is from a mobile device?
Java servlet -如何检测用户是否来自移动设备?
I'm using the TinyMCE javascript editor, and it doesn't work on the iphone. How can I detect if the user is coming from a mobile device?
我正在使用TinyMCE javascript编辑器,它在iphone上不能工作。如何检测用户是否来自移动设备?
6 个解决方案
#1
4
I'm using the TinyMCE javascript editor
我使用的是TinyMCE javascript编辑器
Since you'd like to change the client side behaviour depending on the client, best is to handle this at the client side rather than the server side.
由于您希望根据客户端更改客户端行为,所以最好是在客户端而不是服务器端处理这个问题。
In CSS world, you can hook on the media type to apply styles depending on the media used. Most used media types are screen (usually PCs), handheld (usually mobiles) and print (for the printed page).
在CSS世界中,您可以依赖于使用的媒体类型来应用样式。大多数常用的媒体类型是屏幕(通常是pc)、手持(通常是手机)和打印(打印页面)。
You can make use of it to hide the editor by just the following rule in your CSS:
您可以使用它来隐藏编辑器,只需在CSS中使用以下规则:
@media handheld {
#elementIdContainingEditor { display: none; }
}
You can even specify separate stylesheets depending on the media used.
您甚至可以根据使用的媒体指定单独的样式表。
<link rel="stylesheet" href="default.css" media="screen">
<link rel="stylesheet" href="mobile.css" media="handheld">
If the problem is actually more that it doesn't work because JavaScript is disabled on the particular client, then you better have to execute the particular CSS when JS is disabled:
如果问题更多的是由于JavaScript在特定的客户端上被禁用而无法工作,那么当JS被禁用时,您最好执行特定的CSS:
<noscript><style>#elementIdContainingEditor { display: none; }</style></noscript>
Or the other way round, initially hide it and then show it when JS is enabled:
或者反过来,首先隐藏它,然后在启用JS时显示:
<script>document.getElementById('elementIdContainingEditor').style.display = 'block';</script>
This is more reliable than sniffing the agent in the server side.
这比在服务器端嗅探代理更可靠。
#2
24
I have used the UAgentInfo.java class you can download here (http://code.google.com/p/mobileesp/source/browse/Java/UAgentInfo.java):
我用过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();
}
The UAgentInfo class has a bunch of methods to detect particular devices as well. Just replace detector.detectMobileQuick() for, for instance, detector.detectIphoneOrIpod(), detector.detectKindle(), etc.
UAgentInfo类还有许多方法来检测特定的设备。只需替换detector.detectMobileQuick(),例如,detector.detectIphoneOrIpod()、detector.detectKindle()等。
UPDATE: If you use Spring, you might want to use its native implementation instead. Here is an example: http://spring.io/guides/gs/device-detection/
更新:如果您使用Spring,您可能希望使用它的本机实现。这里有一个例子:http://spring.io/guides/gs/device-detection/
#3
8
Using request.getHeader("User-Agent")
. Here is a list of mobile browsers and their respective User-Agents.
使用request.getHeader(“用户代理”)。这是移动浏览器及其各自的用户代理的列表。
#4
0
The only thing that is different is going to be the User-Agent. Lookup the User agents for the browsers you want to detect. (Not sure why you would care)
唯一不同的是用户代理。查找要检测的浏览器的用户代理。(不知道你为什么会在意)
You could also add some javascript to run something on the browser ?
你也可以添加一些javascript在浏览器上运行?
#5
0
Use the User-Agent in the HTTP request header.
在HTTP请求头中使用用户代理。
request.getHeader("User-Agent")
#6
0
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if(request.getHeader("User-Agent").indexOf("Mobi") != -1) {
} else {
}
}
#1
4
I'm using the TinyMCE javascript editor
我使用的是TinyMCE javascript编辑器
Since you'd like to change the client side behaviour depending on the client, best is to handle this at the client side rather than the server side.
由于您希望根据客户端更改客户端行为,所以最好是在客户端而不是服务器端处理这个问题。
In CSS world, you can hook on the media type to apply styles depending on the media used. Most used media types are screen (usually PCs), handheld (usually mobiles) and print (for the printed page).
在CSS世界中,您可以依赖于使用的媒体类型来应用样式。大多数常用的媒体类型是屏幕(通常是pc)、手持(通常是手机)和打印(打印页面)。
You can make use of it to hide the editor by just the following rule in your CSS:
您可以使用它来隐藏编辑器,只需在CSS中使用以下规则:
@media handheld {
#elementIdContainingEditor { display: none; }
}
You can even specify separate stylesheets depending on the media used.
您甚至可以根据使用的媒体指定单独的样式表。
<link rel="stylesheet" href="default.css" media="screen">
<link rel="stylesheet" href="mobile.css" media="handheld">
If the problem is actually more that it doesn't work because JavaScript is disabled on the particular client, then you better have to execute the particular CSS when JS is disabled:
如果问题更多的是由于JavaScript在特定的客户端上被禁用而无法工作,那么当JS被禁用时,您最好执行特定的CSS:
<noscript><style>#elementIdContainingEditor { display: none; }</style></noscript>
Or the other way round, initially hide it and then show it when JS is enabled:
或者反过来,首先隐藏它,然后在启用JS时显示:
<script>document.getElementById('elementIdContainingEditor').style.display = 'block';</script>
This is more reliable than sniffing the agent in the server side.
这比在服务器端嗅探代理更可靠。
#2
24
I have used the UAgentInfo.java class you can download here (http://code.google.com/p/mobileesp/source/browse/Java/UAgentInfo.java):
我用过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();
}
The UAgentInfo class has a bunch of methods to detect particular devices as well. Just replace detector.detectMobileQuick() for, for instance, detector.detectIphoneOrIpod(), detector.detectKindle(), etc.
UAgentInfo类还有许多方法来检测特定的设备。只需替换detector.detectMobileQuick(),例如,detector.detectIphoneOrIpod()、detector.detectKindle()等。
UPDATE: If you use Spring, you might want to use its native implementation instead. Here is an example: http://spring.io/guides/gs/device-detection/
更新:如果您使用Spring,您可能希望使用它的本机实现。这里有一个例子:http://spring.io/guides/gs/device-detection/
#3
8
Using request.getHeader("User-Agent")
. Here is a list of mobile browsers and their respective User-Agents.
使用request.getHeader(“用户代理”)。这是移动浏览器及其各自的用户代理的列表。
#4
0
The only thing that is different is going to be the User-Agent. Lookup the User agents for the browsers you want to detect. (Not sure why you would care)
唯一不同的是用户代理。查找要检测的浏览器的用户代理。(不知道你为什么会在意)
You could also add some javascript to run something on the browser ?
你也可以添加一些javascript在浏览器上运行?
#5
0
Use the User-Agent in the HTTP request header.
在HTTP请求头中使用用户代理。
request.getHeader("User-Agent")
#6
0
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if(request.getHeader("User-Agent").indexOf("Mobi") != -1) {
} else {
}
}