I need to do a few very simple URL manipulations in Java. Like get the value for a parameter in the query, or update it, ... I was expecting to find a simple utility class doing that in the commons-lang package, but no. I know it is a simple problem, but if there is something already written, why do it again ? Do you know of any ?
I would like to have at least the following capabilities :
String myUrl = "http://www.example.com/test.html?toto=1&titi=2"; // get the value of a parameter String parameterValue = UrlUtils.getParameterValue(myUrl, "toto"); Assert.equals(parameterValue, "1"); // update a parameter String newUrl = UrlUtils.updateParameter(myUrl, "toto", 3); parameterValue = UrlUtils.getParameterValue(myUrl, "toto"); Assert.equals(parameterValue, "3");
Ideally, it would take care of all encoding related issues, and work with java.net.Url as well as with Strings.
Thanks for your help !
A query string is the portion of a URL where data is passed to a web application and/or back-end database. The reason we need query strings is that the HTTP protocol is stateless by design. For a website to be anything more than a brochure, you need to maintain state (store data).
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.
URL Parser provides a quick way to break down URLs into its individual components revealing the scheme, protocol, username, password, hostname, port, domain, subdomain, tld—or top-level domain—path and query string.
I think what you want is called a query string parser instead of an url manipulator and here's one: http://ostermiller.org/utils/CGIParser.java.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With