java resolves the URL request path and parameter key value pairs class (parse out URL request path, including page) _jsp programming

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

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.