Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct a URL with Request Parameters in Java

I've a servlet which is accessed through a URL and that URL has some request parameters in it. Now I need to redirect the user to a different page (from servlet), but also append the request parameters that I got from the request. This is what I'm doing

    StringBuffer sb=new StringBuffer("/test.jsp");
    sb.append( "?" );
    Enumeration en = request.getParameterNames();
    while( en.hasMoreElements() ){
        String paramName = (String) en.nextElement();
        sb.append( paramName );
        sb.append( "=" );
        sb.append(request.getParameter( paramName ));
        sb.append("&");
    }
           String constructedURLWithParams=sb.toString();

But the problem is, there is a "&" which will be added towards the end of the constructed URL. I don't want to again do some string operation and remove the trailing "&". Can you please suggest a better way to do this?

like image 515
Apps Avatar asked Dec 12 '22 11:12

Apps


2 Answers

This is a common scenario. I'm going to simplify it a bit to illustrate some solutions:


Option #1 - remove the last character by changing the StringBuffer length:

StringBuffer sb = new StringBuffer();
Enumeration en = ...
while (en.hasMoreElements())
    sb.append(en.nextElement());
    sb.append(",");
}
if (sb.length() > 0) {
    sb.setLength(sb.length() - 1);
}
System.err.println(sb.toString());

Option #2 - add the separator if the buffer is non empty

StringBuffer sb = new StringBuffer();
Enumeration en = ...
while (en.hasMoreElements())
    if (sb.length() > 0) {
        sb.append(",");
    }
    sb.append(en.nextElement());
}
System.err.println(sb.toString());

Option #3 - add the separator if there are more elements ...

StringBuffer sb = new StringBuffer();
Enumeration en = ...
while (en.hasMoreElements())
    sb.append(en.nextElement());
    if (en.hasMoreElements()) {
        sb.append(",");
    }
}
System.err.println(sb.toString());

Option #4 - add the separator if this is not the first time round the loop ...

StringBuffer sb = new StringBuffer();
Enumeration en = ...
boolean first = true;
while (en.hasMoreElements())
    if (first) {
        first = false;
    } else {
        sb.append(",");
    }
    sb.append(en.nextElement());
}
System.err.println(sb.toString());

Which is best depends on the precise details of what you are doing, and how important performance is.


I should finally note that you need to be a bit more careful when assembling URLs in general and query strings. For instance, you need to properly escape any characters that are not "unreserved" (according to the URL specification) in the parameter names and values. If you are careless, you might end up with a vector for injection of XSS attacks into your website.

like image 74
Stephen C Avatar answered Dec 22 '22 00:12

Stephen C


See this question how to easily build a String from some kind of collection: What's the most elegant way to concatenate a list of values with delimiter in Java?

Also your code has a bug: You have to escape the values since both paramName and the request parameters can contain & and other illegal characters. Use URLEncoder.encode(value, "UTF-8") for that.

like image 30
Aaron Digulla Avatar answered Dec 21 '22 23:12

Aaron Digulla