Package requestpackage; import Java. util. hashmap; import Java. util. map; public class crequest {/*** parse 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 from the URL, leave the request parameter Section * @ Param strurl URL address * @ return URL request parameter 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;}/*** parse the key-value pair in the URL parameter * such as "index. jsp? Action = del & id = 123 ", parse action: del, ID: 123 Save the * @ Param URL address * @ return URL request parameter 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 set of arrsplit = strurlparam. split ("[&]"); For (string strsplit: arrsplit) {Str Ing [] arrsplitequal = NULL; arrsplitequal = strsplit. split ("[=]"); // parse the key value if (arrsplitequal. length> 1) {// parse maprequest correctly. put (arrsplitequal [0], arrsplitequal [1]);} else {If (arrsplitequal [0]! = "") {// Only the parameter has no value. maprequest. Put (arrsplitequal [0], "") ;}} return maprequest;} is not added ;}}
Package requestpackage; import Java. util. map; public class testcrequest {/** is 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 an invalid key is obtained, null system is output. out. println (maprequest. get ("page "));}}
Test code running effect
Index. jsp
Key: ID, value: 123; key: sort, value:; key: Action, value: del;
Null