Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query String Manipulation in Java

Tags:

java

string

Does anyone have, or know of, a java class that I can use to manipulate query strings?

Essentially I'd like a class that I can simply give a query string to and then delete, add and modify query string KVP's.

Thanks in advance.

EDIT

In response to a comment made to this question, the query string will look something like this;

N=123+456+112&Ntt=koala&D=abc

So I'd like to pass this class the query string and say something like;

String[] N = queryStringClass.getParameter("N");

and then maybe

queryStringClass.setParameter("N", N);

and maybe queryStringClass.removeParameter("N");

Or something to that effect.

like image 249
griegs Avatar asked Nov 08 '10 21:11

griegs


People also ask

What is query string manipulation?

Essentially, query string manipulation is as simple as adding alphanumeric characters to a string. It's one of the simplest, most straightforward and most effective types of database hacking around.

What is a query string in Java?

Query String is a group of keywords that send request to the web server. These requests are used to pass information (parameters) from one page to another and you can access those information in receiving page. It containing in the HTTP requests for a specific URL.

How strings are manipulated in Java?

For this, we can simply use the traditional one (the + operator). String str1= “Hello”; String str2 = “India”; String finalResult = str1+” ”+str2; 2. Changing the given string's case: We can change the case of the string whenever required by using toLowerCase() and the toUpperCase() Java built-in functions.

How do you create a query string in Java?

To create a string-based query, specify the attribute names of entity classes directly as strings, rather than the attributes of the metamodel class. For example, this query finds all Pet entities where the value of the name attribute is Fido: CriteriaQuery<Pet> cq = cb. createQuery(Pet.


2 Answers

SOmething like this

 public static Map<String, String> getQueryMap(String query)  
 {  
     String[] params = query.split("&");  
     Map<String, String> map = new HashMap<String, String>();  
     for (String param : params)  
     {  
         String name = param.split("=")[0];  
         String value = param.split("=")[1];  
         map.put(name, value);  
     }  
     return map;  
 }  

To iterate the map simply:

 String query = url.getQuery();  
 Map<String, String> map = getQueryMap(query);  
 Set<String> keys = map.keySet();  
 for (String key : keys)  
 {  
    System.out.println("Name=" + key);  
    System.out.println("Value=" + map.get(key));  
 }  
like image 134
Maurizio Cucchiara Avatar answered Sep 20 '22 16:09

Maurizio Cucchiara


You can also use Google Guava's Splitter.

String queryString = "variableA=89&variableB=100";
Map<String,String> queryParameters = Splitter
    .on("&")
    .withKeyValueSeparator("=")
    .split(queryString);
System.out.println(queryParameters.get("variableA"));

prints out

89

This I think is a very readable alternative to parsing it yourself.

Edit: As @raulk pointed out, this solution does not account for escaped characters. However, this may not be an issue because before you URL-Decode, the query string is guaranteed to not have any escaped characters that conflict with '=' and '&'. You can use this to your advantage in the following way.

Say that you must decode the following query string:

a=%26%23%25!)%23(%40!&b=%23%24(%40)%24%40%40))%24%23%5E*%26

which is URL encoded, then you are guaranteed that the '&' and '=' are specifically used for separating pairs and key from value, respectively, at which point you can use the Guava splitter to get:

a = %26%23%25!)%23(%40!
b = %23%24(%40)%24%40%40))%24%23%5E*%26

Once you have obtained the key-value pairs, then you can URL decode them separately.

a = &#%!)#(@!
b = #$(@)$@@))$#^*&

That should cover all cases.

like image 24
laughing_man Avatar answered Sep 19 '22 16:09

laughing_man