You will need to use standard Java API to generate the validation image. Then you will show that image to the user for validation. And since you know what you wrote to the image, you can verify it easily!
And check
these tips to see which API calls you will use basically..