Adding Captcha Fields
To use captcha validation support in the actions, the checkCaptcha
parameter needs to be set
to true
in the Struts action configuration. When this parameter is set,
the action enforces captcha validation. For example, if we want to add captcha validation to the
/blog/add
action which is an instance of
the class com.ndc.usercontent.struts.actions.save.SaveArticle
, we need the following configuration.
<action path="/blog/add" name="articleForm" parameter="articleType=blog;checkCaptcha=true" type="com.ndc.usercontent.struts.actions.save.SaveArticle"> <!-- Other action configuration stuff --> </action>
Once the above configuration is in place, we can write JSP segment as part of an HTML form
to show the captcha challenge interface.
For example, we want to add a captcha field to the the /blog/add
action . To do this, we need to
add the following JSP segment in the HTML form that will allow a user to create a blog.
-
For ReCaptcha
<html:form action="/blog/add"> <!-- JSP code to render the form properties related to creating a blog --> <div class="captcha"> <captcha:recaptchaHTML theme="red" lang="en"/> </div> <!-- The rest of the JSP code to render the form properties related to creating a blog goes here. --> </html:form>
<captcha:recaptchaHTML/> tag prints the HTML fragment to show ReCaptcha interface
-
For JCaptcha
<html:form action="/blog/add"> <!-- JSP code to render the form properties related to creating a blog --> <div class="captcha"> <label for="jcaptcha_response">Verification code: </label> <input id="jcaptcha_response" type="text" name="jcaptcha_response" /><br/> <html:image page="/jcaptcha.do"/> </div> <!-- The rest of the JSP code to render the form properties related to creating a blog goes here. --> </html:form>
Note the
input
element namedjcaptcha_response
. This field should contain the code displayed in the captcha image. The tag<html:image page="/jcaptcha.do"/>
creates the HTML elementimg
with appropriate image source URL, which in turn pulls the image from the/jcaptcha
action.
When the user's input captcha code is wrong, the action will direct the user to the configured error page (which in common case is the same page where the user is filling the form). Have a look at Creating Articles to get an overview on how to configure the error URL. Here is the JSP fragment to show captcha error in error page:
<logic:messagesPresent message="true" property="CAPTCHA"> <p class="fieldError"> <html:messages id="error" message="true" bundle="Validation" property="CAPTCHA"> <bean:write name="error"/><br /> </html:messages> </p> </logic:messagesPresent>
The following VCE Struts actions come with built-in support for captcha verification:
-
com.ndc.usercontent.struts.actions.save.SaveArticle
-
com.ndc.usercontent.struts.actions.save.SaveGroupProfile
-
com.ndc.usercontent.struts.actions.save.SaveUserProfile
Captcha support can be used for the Forum actions in a similar way, if you have configured properly as stated in VCE Captcha Support for Forum
If you want to use captcha provider support for other actions, you can write a generic servlet filter through which the request that requires captcha verification must go through.