tags:

views:

566

answers:

2

Hi,

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.

+2  A: 

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.

darren
@darren: Hi, thanks for replying. I saw that link you have provided with but then that is the example for Sticky Captcha, which needs to include session, and there isn't customization. I only need a Simple Captcha, and some customization on the captcha image using its builder. Moreover, I really need help to put that in a servlet, as my validation portion are all in a servlet, instead of jsp.
jl
+3  A: 

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.

Pascal Thivent