JavaWeb:后台判断是手机登陆还是Pc登陆

2024-11-08 02:40:05
推荐回答(2个)
回答(1):

两种方式:第一种用在页面,第二种用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
}
}

回答(2):

额,这个就靠数据库了,你可以在客户端登录后,服务器数据库上将该用户的某个字段值设置为表示已登录状态的值,之后要判断是否登录就查询这个值了