JSP to judge the regular _jsp programming of mobile devices

Source: Internet
Author: User
Tags alphanumeric characters
See a very good article, "in the sky Cat, what is the front end?" ", there are days in the cat PHP to determine the mobile device (personal guess), feel very good, so decided to migrate to the JSP inside.

The JSP file name is index.jsp, actually also can use the filter to intercept, then jumps to other domain names to go.

The complete code is as follows:
Copy Code code as follows:

<% @page import= "Java.util.regex.Matcher"%>
<% @page import= "Java.util.regex.Pattern"%>
<%@ page language= "java" pageencoding= "UTF-8"%>
<%!

\b is a logical interval between a word boundary (a string of two letters and non-alphanumeric characters),
The string is coded once at compile time, so it is "\\b"
\b is the logical interval between the internal logical intervals of a word (two alphabetic characters attached).
String Phonereg = "\\b (IP (hone|od) |android|opera m (ob|in) i"
+ "|windows (phone|ce) |blackberry"
+ "|s (Ymbian|eries60|amsung) |p" (LAYBOOK|ALM|ROFILE/MIDP)
+ "|laystation portable) |nokia|fennec|htc[-_]"
+ "|mobile|up.browser| [1-4] [0-9] {2}x[1-4][0-9]{2}) \\b ";
String Tablereg = "\\b (ipad|tablet| ( Nexus 7) |up.browser "
+"| [1-4] [0-9] {2}x[1-4][0-9]{2}) \\b ";
Pattern Phonepat = Pattern.compile (Phonereg, pattern.case_insensitive);
Pattern Tablepat = Pattern.compile (Tablereg, pattern.case_insensitive);

public boolean checkmobile (String useragent) {
if (null = = useragent) {
useragent = "";
}
The
Matcher Matcherphone = Phonepat.matcher (useragent);
Matcher matchertable = Tablepat.matcher (useragent);
if (Matcherphone.find () | | Matchertable.find ()) {
return true;
} else {
return false;
}
}
%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

//
String useragent = Request.getheader ("User-agent"). toLowerCase ();

if (null = = useragent) {
useragent = "";
}
if (Checkmobile (useragent)) {
Response.sendredirect (basepath+ "download.html");
Request.getrequestdispatcher ("/download.html"). Forward (Request,response);
} else {
Response.sendredirect (basepath+ "index.html");
Request.getrequestdispatcher ("/index.html"). Forward (Request,response);
}
//
%>

<! DOCTYPE html>
<base href= "<%=basePath%>" >

<title> Test mobile device Jump </title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "test, mobile device, jump" >
<meta http-equiv= "description" content= "Test mobile device Jump" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-->

<body>

<div id= "PageContent" style= "min-height:500px;_height:500px;" >

It's running! <br>
</div>

</body>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.