博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
15.9. FAQ
阅读量:6257 次
发布时间:2019-06-22

本文共 2367 字,大约阅读时间需要 7 分钟。

15.9.1. Struts 怎样判断用户来自电脑还是移动设备

package cn.netkiller.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import javax.servlet.http.HttpServletRequest;public class Index extends ActionSupport {	private static final long serialVersionUID = 7782483098148890753L;	public String execute() {		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);		String userAgent = request.getHeader("User-Agent").toLowerCase();		System.out.println(this.getClass().getName() + ": " + userAgent);		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-", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", "Googlebot-Mobile" };		if (userAgent != null) {			for (String mobileAgent : mobileAgents) {				if (userAgent.indexOf(mobileAgent) >= 0) {					return "mobile";				}			}		}		return "computer";	}}
index
/computer
http://m.netkiller.cn/index.html
 

原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
小波变换简单介绍(2)
查看>>
Dubbo -- 系统学习 笔记 -- 示例 -- 线程模型
查看>>
Dubbo -- 系统学习 笔记 -- 示例 -- 多注册中心
查看>>
使用C++实现学生管理系统
查看>>
BroadcastReceiver类
查看>>
大杂烩 -- 查找单向链表倒数第m个元素
查看>>
SQL笔记 --- 数据库设计步骤(转)
查看>>
cocos2d-iphone 动作
查看>>
[Preact] Integrate react-router with Preact
查看>>
函数指针的说明
查看>>
Python操作redis学习系列之(集合)set,redis set详解 (六)
查看>>
Linux时间子系统之二:表示时间的单位和结构
查看>>
leetcode 221: Maximal Square
查看>>
Oracle更新时间字段
查看>>
Android 四大组件学习之ContentProvider二
查看>>
使用jcaptcha插件生成验证码
查看>>
centos6.5 (linux) 禁用模块 IPV6模块的方法
查看>>
用webpack2.0构建vue2.0超详细精简版
查看>>
从分类,排序,top-k多个方面对推荐算法稳定性的评价
查看>>
006_ssl监测及评分
查看>>