Question

I am a newbie at java/java servlet. I need the simpleCaptcha for a form, using html and javaservlet for the code. With reference to http://simplecaptcha.sourceforge.net/extending.html.

Captcha captcha = new Captcha.Builder(200, 50)
    .addText()
    .addBackground()
    .addNoise()
    .gimp()
    .addBorder()
    .build(); // Required. 

Where (java servlet) should I put this piece of code in order to create the captcha on html?

Thank you very much.

Was it helpful?

Solution

To extend SimpleCaptcha and customize your CAPTCHA, my understanding is that you'll have to create your own HttpServlet (maybe extends SimpleCaptchaServlet). To do so, I suggest to download the source code and to look at SimpleCaptchaServlet or StickyCaptchaServlet. This is what the doGet() method of SimpleCaptchaServlet looks like:

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    Captcha captcha = new Captcha.Builder(_width, _height)
        .addText()
        .addBackground(new GradiatedBackgroundProducer())
        .gimp()
        .addNoise()
        .addBorder()
        .build();

    CaptchaServletUtil.writeImage(resp, captcha.getImage());

    req.getSession().setAttribute(NAME, captcha);
}

This should be self-explaining: create your own servlet and put your custom Captcha Builder code in the doGet() method. Then, follow the instructions of the Installing section but, instead of using one of their servlet, declare yours in the web.xml. Finally, package/deploy your application. An example is bundled in the source distribution under examples. Check it out if you need more guidance about the structure, the dependencies and the packaging of your web application.

OTHER TIPS

hey have you had a look at this page yet? http://simplecaptcha.sourceforge.net/installing.html

this is about as straight forward as it gets I think. This project gives you a few Capcha servlets out of the box. You just have to map them in your web.xml file. You can follow along and create the jsp that will call them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top