If you have an img element to display the captcha, then the key to refreshing its image is to change its src attribute to something different.
If src is not changed, a browser would display the cached version of that URL, and you'll see the same image every time which is not what you want.
But if src is changed, a browser would then asynchronously and automatically make a new request to that changed URL, and display whatever image bytes it receives based on the response MIME type.
One simple way to change src attribute without changing the
servlet behind that URL is to add current time as a query parameter on each request.
Your client side javascript should be something like:
The
http://your_app_url/generatecaptcha.do is the URL for SimpleCaptchaServlet (or your custom captcha generating servlet) which responds with image bytes with "image/png" MIME type. The t query parameter can be ignored on server side - its purpose is just to vary the URL.