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

发布网友 发布时间:2022-04-23 07:02

我来回答

2个回答

热心网友 时间:2022-06-17 04:46

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

热心网友 时间:2022-06-17 04:47

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

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com