This code was actually not discovered by me, but was accidentally discovered when looking through the company project. I thought it was very practical, so I took it out and shared it with everyone.
Framework: Struts2+spring+ibatis,
The main implementation is to rely on "User-Agent" in the Http request header to complete it. OK, I will just upload the code without saying much nonsense.
In Action:
public String execute() { HttpServletRequest request = ServletActionContext.getRequest(); boolean isMoblie = JudgeIsMoblie(request); if(isMoblie){ return "phone"; } return "pc"; } public boolean JudgeIsMoblie(HttpServletRequest request) { 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", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "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-", "sie-", "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) { <span style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">//This baby says that he doesn't understand its internal principles very well, but just knows the general meaning. </span> if (request.getHeader("User-Agent").toLowerCase() .indexOf(mobileAgent) >= 0) { isMoblie = true; break; } } } return isMoblie; }Struts2
<span style="white-space:pre"> </span><action name="name"> <result name="pc">download/main.jsp</result> <result name="phone">download/productPH.jsp</result> </action>
Okay, there is only so much code, there is nothing left. . . Doesn't it feel very simple?
Although it was not written by me, I also gained something when I saw this code. In fact, many settings are included in the request header, because no matter what browser or device it is, it needs to call a request through http, so it must have an http protocol. As long as it is proficient in the http protocol, everything will be easy to do (it means that I don’t understand the http protocol at the moment, and then I will study hard and then pretend to be good)~~
The above example of judging that the client is a mobile phone/PC and returning to different pages is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.