Java parses the URL request path and parameter key-Value Pair class

Source: Internet
Author: User
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

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.