tags:

views:

69

answers:

3

Hi,

Do you know any JDK service in Java 1.5 that knows to take care of URL appending? (taking care of putting "?" or "&" depends on the fact that the query param is the first one or not).

Thanks, Shay

+1  A: 
public String buildUrl(String url, List<String> params) {
    StringBuilder builder = new StringBuilder(url);
    if(params != null && params.size() > 0) {
        builder.append("?");

        for(Iterator<String> i = params.iterator(); i.hasNext();) {
            String s = i.next();
            builder.append(s);
            if(i.hasNext()) {
                builder.append("&");
            }
        }
    }

    return builder.toString();
}
Noel M
A: 

As far as I know, there's one called JavaService which allows server-type Java Programs to run on Windows platform. I don't know whether it could satisfy your need.

There's an alternative way that you can code the functionality for your necessity, then make it as a Windows Service. I've once read an article at DevX and an article titled Running Java Applications as a Windows Service.

venJava
A: 

A rather straightforward (and very similar to Noel M's example who posted it while I'm writing) way would be:

StringBuilder sb = new StringBuilder(url);
url.indexOf("?") > -1 ? sb.append("&") : sb.append("?");

// loop over your paramaters and append them like in Noel M's example

url = sb.toString();
André van Toly