JavaWeb:后台判断是手机登陆还是Pc登陆
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb:后台判断是手机登陆还是Pc登陆相关的知识,希望对你有一定的参考价值。
两种方式:第一种用在页面,第二种用java过滤器页面用jq判断,单独建立一个js文件,哪个页面使用,就引入哪个页面,一般为首页。引用时,记得先引用jquery
$(function()
var tag = isMobile(); // true为PC端,false为手机端
if (tag)
alert("手机");
console.info("手机")
);
function isMobile()
var userAgentInfo = navigator.userAgent;
var mobileAgents = [ "android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod" ];
var mobile_flag = false;
// 根据userAgent判断是否是手机
for (var v = 0; v < mobileAgents.length; v++)
if (userAgentInfo.indexOf(mobileAgents[v]) > 0)
console.info("chenk userAgent")
mobile_flag = true;
break;
var screen_width = window.screen.width;
var screen_height = window.screen.height;
// 根据屏幕分辨率判断是否是手机
if (screen_width < 500 && screen_height < 800)
console.info("check screen")
mobile_flag = true;
return mobile_flag;
过滤器方式:filter
public class IsMobile implements Filter
/**
* Default constructor.
*/
public boolean IsMobileCheck(ServletRequest sRequest)
HttpServletRequest request = (HttpServletRequest)sRequest;
boolean isMoblie = false;
String[] mobileAgents = "iphone", "android", "phone", "mobile",
"wap", "netfront", "java", "opera mobi", "opera mini", "ucweb",
"windows ce", "symbian", "series", "webos", "sony",
"blackberry", "dopod", "nokia", "samsung", "palmsource", "xda",
"pieplus", "meizu", "midp", "cldc", "motorola", "foma",
"docomo", "up.browser", "up.link", "blazer", "helio", "hosin",
"huawei", "novarra", "coolpad", "webos", "techfaith",
"palmsource", "alcatel", "amoi", "ktouch", "nexian",
"ericsson", "philips", "sagem", "wellcom", "bunjalloo", "maui",
"smartphone", "iemobile", "spice", "bird", "zte-", "longcos",
"pantech", "gionee", "portalmmm", "jig browser", "hiptop",
"benq", "haier", "^lct", "320x320", "240x320", "176x220",
"w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq",
"bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang",
"doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi",
"keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo",
"midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-",
"newt", "noki", "oper", "palm", "pana", "pant", "phil", "play",
"port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-",
"send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar",
"sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-",
"upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp",
"wapr", "webc", "winw", "winw", "xda", "xda-",
"Googlebot-Mobile" ;
if (request.getHeader("User-Agent") != null)
for (String mobileAgent : mobileAgents)
if (request.getHeader("User-Agent").toLowerCase()
.indexOf(mobileAgent) >= 0)
isMoblie = true;
break;
return isMoblie;
/**
* @see Filter#destroy()
*/
public void destroy()
// TODO Auto-generated method stub
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest sRequest, ServletResponse response, FilterChain chain) throws IOException, ServletException
System.out.println("手机检查");
HttpServletRequest request = (HttpServletRequest)sRequest;
if (IsMobileCheck(request))
((HttpServletResponse)response).sendRedirect(request.getContextPath() + "/wap/index.jsp");
return;
else
chain.doFilter(request, response);
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException
// TODO Auto-generated method stub
参考技术A 额,这个就靠数据库了,你可以在客户端登录后,服务器数据库上将该用户的某个字段值设置为表示已登录状态的值,之后要判断是否登录就查询这个值了
JS判断登陆端是PC还是手机
读取navigator.userAgent里面的信息,为了方便利用toLowerCase方法转成小写的形式。然后用MATCH方法进行匹配版本信息,这里提供了多个版本的测试信息,可以用来做后续代码的接口。这里作统一或起来判断登陆端是否是手机~~程序很简单,主要还是丰富JS知识吧。下面是个简单的小例子,希望对你有帮助。
<!doctype html> <html> <script type="text/javascript"> function browserRedirect() { var sUserAgent= navigator.userAgent.toLowerCase(); var bIsIpad= sUserAgent.match(/ipad/i) == "ipad"; var bIsIphoneOs= sUserAgent.match(/iphone os/i) == "iphone os"; var bIsMidp= sUserAgent.match(/midp/i) == "midp"; var bIsUc7= sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; var bIsUc= sUserAgent.match(/ucweb/i) == "ucweb"; var bIsAndroid= sUserAgent.match(/android/i) == "android"; var bIsCE= sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM= sUserAgent.match(/windows mobile/i) == "windows mobile"; if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) { document.getElementById("a").style.display="block"; document.getElementById("b").style.display="none"; } else { document.getElementById("b").style.display="block"; document.getElementById("a").style.display="none"; } } window.onload=function(){browserRedirect();} </script> <meta charset="utf-8"/> <head> <title>web1</title> </head> <body> <div id="a"><p>这是手机</p></div> <div id="b"><p>这是电脑</p></div> </body> </html>
以上是关于JavaWeb:后台判断是手机登陆还是Pc登陆的主要内容,如果未能解决你的问题,请参考以下文章