package com.bml.icbs.ws.param;
import java.util.HashMap;
public class RequestParameter {
private String param;
private HashMap<String, String> hashmap;
public RequestParameter(String param){
this.param=param;
this.hashmap = new HashMap<String, String>();
Integer i;
String key,value;
String myArray[] = this.param.split("&");
for (i=0;i<myArray.length;i++){
key=myArray[i].substring(0,myArray[i].indexOf("="));
value=myArray[i].substring(myArray[i].indexOf("=")+1);
this.hashmap.put(key,value);
}
}
public String getParameterValue(String key){
return this.hashmap.get(key);
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public HashMap<String, String> getHashmap() {
return hashmap;
}
public void setHashmap(HashMap<String, String> hashmap) {
this.hashmap = hashmap;
}
}
When I Test the above method in case the URL is "param2=ffffhhh¶m1=oooo¶m3=pppp¶m4=iiii¶m5=kkkkk"
The result is:
SIZE 5
URL param2=ffffhhh¶m1=oooo¶m3=pppp¶m4=iiii¶m5=kkkkk
HASHMAP {param1=oooo, param2=ffffhhh, param3=pppp, param4=iiii, param5=kkkkk}
PARAM1 = oooo
which is correct
public static void main(String[] args) {
RequestParameter reqParam = new RequestParameter("param2=ffffhhh¶m1=oooo¶m3=pppp¶m4=iiii¶m5=kkkkk");
System.out.println("SIZE " + reqParam.getHashmap().size());
System.out.println("URL " + reqParam.getParam());
System.out.println("HASHMAP " + reqParam.getHashmap());
System.out.println("PARAM1 = " reqParam.getParameterValue("param1"));
}
but In case I pass the URL as the following one: "param2=ffff&&&hhh=¶m1=oooo=&¶m3=pppp¶m4=iiii&&yy=¶m5=kkkkk"
public static void main(String[] args) {
RequestParameter reqParam = new RequestParameter("param2=ffff&&&hhh=¶m1=oooo=&¶m3=pppp¶m4=iiii&&yy=¶m5=kkkkk");
System.out.println("SIZE " + reqParam.getHashmap().size());
System.out.println("URL " + reqParam.getParam());
System.out.println("HASHMAP " + reqParam.getHashmap());
System.out.println("PARAM1 = " reqParam.getParameterValue("param1"));
}
it throws the following exception: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1937) at com.bml.icbs.ws.param.RequestParameter.(RequestParameter.java:188) at com.bml.icbs.ws.param.main.main(main.java:9)