package cn.com.yyg.front.filter; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.filter.OncePerRequestFilter; public class MobileOrPcFilter extends OncePerRequestFilter { /** * pc mobile 判斷 * * @param request * @return * @author linwk 2016年9月11日 */ public boolean isMoblie(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", "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; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); HttpServletRequest httpRequest = (HttpServletRequest) request; String uri = httpRequest.getRequestURI(); String mobileString = (String) httpRequest.getSession().getAttribute("mobile"); // TODO 資源 if (StringUtils.contains(uri, "assets")) { filterChain.doFilter(request, response);// 放行 return; } if (isMoblie(httpRequest)) { httpRequest.getRequestDispatcher("/wap" + uri).forward(request, response); } else { filterChain.doFilter(request, response);// 電腦放行 } } }
web.xml :css
<!-- 電腦手機過濾器 --> <filter> <filter-name>wapfilter</filter-name> <filter-class>cn.com.yyg.front.filter.MobileOrPcFilter</filter-class> </filter> <filter-mapping> <filter-name>wapfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
效果: 咱們攔截全部的請求(懶漢作法(●ˇ∀ˇ●)),在Filter的代碼中咱們能夠看到,判斷爲手機的UA後實現轉發,而且是轉發到wap文件夾下拼接請求的jsp的名字;下面給出一個拼接路徑的對照表: 電腦地址 拼接wap後的手機網址html
/index.jsp /wap/index.jsp /about.jsp /wap/about.jsp
因此咱們還須要在wap目錄下建立文件名同樣(h5頁面)的jsp文件(也能夠考慮放到WEB-INF下隱藏訪問),這樣咱們手機在訪問時,就會被轉發到指定的jsp文件,從而實現地址不變,內容改變的效果。 這是我在作網站的一個小小的思路,但願也能幫助到有須要的朋友,若是你有更好的思路或建議,也歡迎評論中提出。java