Build a URL with request parameters in Java

I have a servlet accessed via a url, and this url has some request parameters. Now I need to redirect the user to another page (from the servlet), but also add the request parameters that I received from the request. This is what I do

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 that there is an β€œ&” that will be added to the end of the constructed URL. I do not want to perform some string operation again and delete the trailing "&". Can you suggest a better way to do this?

+4
source share
3 answers

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

If it is not, your approach seems fine. Just

 if (en.hasMoreElements()) sb.append("&"); 
+3
source

This is a common scenario. I will simplify it a little to illustrate some solutions:


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

 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 number 2 - add a separator if the buffer is not 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 number 3 - add a 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 number 4 - add a separator, if this is not the first time around 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 best depends on accurate data about what you are doing and how important performance is.


Lastly, I should point out that you need to be more careful when compiling URLs in general and query strings. For example, you need to correctly avoid any characters that are not "unconditional" (according to the specification of the URL) in the names and values ​​of parameters. If you are careless, you may have a vector for injecting XSS attacks on your site.

+6
source

Have a look at this question, how easy it is to build a string from some collection: What is the most elegant way to combine a list of values ​​with a separator in Java?

Also your code has an error: you need to avoid the values, since both paramName and query parameters can contain & and other invalid characters. Use URLEncoder.encode(value, "UTF-8") for this.

+4
source

All Articles