开发者

Construct a URL with Request Parameters in Java

开发者 https://www.devze.com 2023-03-18 00:48 出处:网络
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 paramete

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

    St开发者_开发技巧ringBuffer 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?


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.


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.


Simply append "?" + request.getQueryString() (if the parameters are passed in the URL - the query string contains all the get parameters)

If they aren't, then your approach seems fine. Just have

if (en.hasMoreElements()) sb.append("&");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号