java parses the path requested by url and the parameter key value pairs of parses the path requested by url including the page

  • 2020-05-19 05:30:19
  • OfStack

 
package RequestPackage; 
import java.util.HashMap; 
import java.util.Map; 
public class CRequest { 
/** 
*  Resolve the url The requested path, including the page  
* @param strURL url address  
* @return url The 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; 
} 
/** 
*  To get rid of url , leaving the request parameters section  
* @param strURL url address  
* @return url Request parameters section  
*/ 
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; 
} 
/** 
*  Resolve the url Key value pairs in the parameter  
*  Such as  "index.jsp?Action=del&id=123" And analyze the Action:del,id:123 deposit map In the  
* @param URL url address  
* @return url Request parameters section  
*/ 
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 has a value of 1 group  
arrSplit=strUrlParam.split("[&]"); 
for(String strSplit:arrSplit) 
{ 
String[] arrSplitEqual=null; 
arrSplitEqual= strSplit.split("[=]"); 
// Parse out the key  
if(arrSplitEqual.length>1) 
{ 
// Resolved properly  
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]); 
} 
else 
{ 
if(arrSplitEqual[0]!="") 
{ 
// Only the parameters have no value, do not add  
mapRequest.put(arrSplitEqual[0], ""); 
} 
} 
} 
return mapRequest; 
} 
} 

The test class
 
package RequestPackage; 
import java.util.Map; 
public class TestCRequest { 
/** Used for testing 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 pairs  
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); 
// Get invalid key, output null 
System.out.println(mapRequest.get("page")); 
} 
} 

Test how the code works
index.jsp
key:id,Value:123;key:sort,Value:;key:action,Value:del;
null

Related articles: