How to replace a query string in an Apache Velocity template?
<p>In my web application I'm trying to prevent users from inserting JavaScript in the freeText parameter when they're running a search. </p>

<p>To do this, I've written code in the header Velocity file to check whether the query string contains a parameter called freeText, and if so, use the replace method to replace the characters within the parameter value.  However, when you load the page, it still displays the original query string - I'm unsure on how to replace the original query string with my new one which has the replaced characters.  </p>

<p>This is my code:</p>

<pre><code>#set($freeTextParameter = "$request.getParameter('freeText')")
freeTextParameter: $freeTextParameter

#if($freeTextParameter)
    ##Do the replacement: 
    #set($replacedQueryString = "$freeTextParameter.replace('confirm','replaced')")
    replacedQueryString after doing the replace: $replacedQueryString
    The query string now: $request.getQueryString()
    The freeText parameter now: $request.getParameter('freeText')
#end
</code></pre>

<p>In the code above, the replacedQueryString variable has changed as expected (ie the replacement has been carried out as expected), but the $request.getQueryString() and $request.getParameter('freeText') are still the same as before, as if the replacement had never happened.</p>

<p>Seeing as there is a request.getParameter method which works fine for getting the parameters, I assumed there would be a request.setParameter method to do the same thing in reverse, but there isn't.  </p>