Analyze URL, I want to use regular expressions, but the regular expression speed is slow. Just process it with split
Copy codecode is as follows:
package RequestPackage;
import java.util.HashMap;
import java.util.Map;
public class CRequest {
/**
* Analyze the path of the URL request, including the page
* @param Strurl URL Address
* @Return url path
*/
public static String UrlPage(String strURL)
{
String strPage=null;
String[] arrSplit=null;
strURL=strURL.trim().toLowerCase();
arrSplit=strURL.split(“[?]”);
if(strURL.length()>0)
{
if(arrSplit.length>1)
{
if(arrSplit[0]!=null)
{
strPage=arrSplit[0];
}
}
}
return strPage;
}
/**
* Remove the path in the URL and leave the request parameter part
* @param Strurl URL Address
* @Return url request parameter part
*/
private static String TruncateUrlPage(String strURL)
{
String strAllParam=null;
String[] arrSplit=null;
strURL=strURL.trim().toLowerCase();
arrSplit=strURL.split(“[?]”);
if(strURL.length()>1)
{
if(arrSplit.length>1)
{
if(arrSplit[1]!=null)
{
strAllParam=arrSplit[1];
}
}
}
return strAllParam;
}
/**
* Analyze the key value pair in the URL parameter
* such as “Index.jsp? Action = Del & ID = 123”, analyzes Action: Del, ID: 123 Start in Map
* @param url url address
* @Return url request parameter part
*/
public static Map<String, String> URLRequest(String URL)
{
Map<String, String> mapRequest = new HashMap<String, String>();
String[] arrSplit=null;
String strUrlParam=TruncateUrlPage(URL);
if(strUrlParam==null)
{
return mapRequest;
}
// Each key value is a group
arrSplit=strUrlParam.split(“[&]”);
for(String strSplit:arrSplit)
{
String[] arrSplitEqual=null;
arrSplitEqual= strSplit.split(“[=]”);
// Analyze the key value
if(arrSplitEqual.length>1)
{
// Correct analysis
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
}
else
{
if(arrSplitEqual[0]!=””)
{
// Only the parameters are not worth it, not added
mapRequest.put(arrSplitEqual[0], “”);
}
}
}
return mapRequest;
}
}
import java.util.HashMap;
import java.util.Map;
public class CRequest {
/**
* Analyze the path of the URL request, including the page
* @param Strurl URL Address
* @Return url path
*/
public static String UrlPage(String strURL)
{
String strPage=null;
String[] arrSplit=null;
strURL=strURL.trim().toLowerCase();
arrSplit=strURL.split(“[?]”);
if(strURL.length()>0)
{
if(arrSplit.length>1)
{
if(arrSplit[0]!=null)
{
strPage=arrSplit[0];
}
}
}
return strPage;
}
/**
* Remove the path in the URL and leave the request parameter part
* @param Strurl URL Address
* @Return url request parameter part
*/
private static String TruncateUrlPage(String strURL)
{
String strAllParam=null;
String[] arrSplit=null;
strURL=strURL.trim().toLowerCase();
arrSplit=strURL.split(“[?]”);
if(strURL.length()>1)
{
if(arrSplit.length>1)
{
if(arrSplit[1]!=null)
{
strAllParam=arrSplit[1];
}
}
}
return strAllParam;
}
/**
* Analyze the key value pair in the URL parameter
* such as “Index.jsp? Action = Del & ID = 123”, analyzes Action: Del, ID: 123 Start in Map
* @param url url address
* @Return url request parameter part
*/
public static Map<String, String> URLRequest(String URL)
{
Map<String, String> mapRequest = new HashMap<String, String>();
String[] arrSplit=null;
String strUrlParam=TruncateUrlPage(URL);
if(strUrlParam==null)
{
return mapRequest;
}
// Each key value is a group
arrSplit=strUrlParam.split(“[&]”);
for(String strSplit:arrSplit)
{
String[] arrSplitEqual=null;
arrSplitEqual= strSplit.split(“[=]”);
// Analyze the key value
if(arrSplitEqual.length>1)
{
// Correct analysis
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
}
else
{
if(arrSplitEqual[0]!=””)
{
// Only the parameters are not worth it, not added
mapRequest.put(arrSplitEqual[0], “”);
}
}
}
return mapRequest;
}
}
Test class
Copy codecode is as follows:
package RequestPackage;
import java.util.Map;
public class TestCRequest {
/** Used to test the Crequest class
* @param args
*/
public static void main(String[] args) {
// Request URL
String str = “index.jsp?Action=del&id=123&sort=”;
// URL page path
System.out.println(CRequest.UrlPage(str));
// URL parameter key value pair
String strRequestKeyAndValues=””;
Map<String, String> mapRequest = CRequest.URLRequest(str);
for(String strRequestKey: mapRequest.keySet()) {
String strRequestValue=mapRequest.get(strRequestKey);
strRequestKeyAndValues+=”key:”+strRequestKey+”,Value:”+strRequestValue+”;”;
}
System.out.println(strRequestKeyAndValues);
// When getting an invalid key, output NULL
System.out.println(mapRequest.get(“page”));
}
}
import java.util.Map;
public class TestCRequest {
/** Used to test the Crequest class
* @param args
*/
public static void main(String[] args) {
// Request URL
String str = “index.jsp?Action=del&id=123&sort=”;
// URL page path
System.out.println(CRequest.UrlPage(str));
// URL parameter key value pair
String strRequestKeyAndValues=””;
Map<String, String> mapRequest = CRequest.URLRequest(str);
for(String strRequestKey: mapRequest.keySet()) {
String strRequestValue=mapRequest.get(strRequestKey);
strRequestKeyAndValues+=”key:”+strRequestKey+”,Value:”+strRequestValue+”;”;
}
System.out.println(strRequestKeyAndValues);
// When getting an invalid key, output NULL
System.out.println(mapRequest.get(“page”));
}
}
Test code running effect
index.jsp
key:id,Value:123;key:sort,Value:;key:action,Value:del;
null