Copy Code code as follows:
Package requestpackage;
Import Java.util.HashMap;
Import Java.util.Map;
public class Crequest {
/**
* resolves the URL request path, 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, leaving the request parameter part
* @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;
}
/**
* Resolves the key value pairs in the URL parameter
* like "index.jsp"? Action=del&id=123 ", parse out action:del,id:123 into map
* @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 value is a group
Arrsplit=strurlparam.split ("[;]");
for (String strsplit:arrsplit)
{
String[] Arrsplitequal=null;
Arrsplitequal= strsplit.split ("[=]");
Parse out key values
if (arrsplitequal.length>1)
{
Correct resolution
Maprequest.put (Arrsplitequal[0], arrsplitequal[1]);
}
Else
{
if (arrsplitequal[0]!= "")
{
Only parameters have no value, do not join
Maprequest.put (Arrsplitequal[0], "");
}
}
}
return maprequest;
}
}
Test class
Copy Code code as follows:
Package requestpackage;
Import Java.util.Map;
public class Testcrequest {
/** for testing crequest classes
* @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 you get an invalid key, the output is null
System.out.println (Maprequest.get ("page"));
}
}
Test code Run Effect
index.jsp
Key:id,value:123;key:sort,value:;key:action,value:del;
Null