Hi Friend,
Here i want to explain how to add captch image in custom portlet & refresh that captch .So i explain step by step.
First in Jsp File Write below code .
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
jQuery("#refreshCaptchaActivity").click(function()
{
jQuery(".captcha").attr("src", jQuery(".captcha").attr("src")+"&force=" + new Date().getMilliseconds());
return false;
});
</script>
<div>
<portlet:resourceURL var="captchaURL" />
<liferay-ui:captcha url="<%=captchaURL%>" />
<div style="padding: 23px 0 0 5px; cursor: pointer;">
<img id="refreshCaptchaActivity"
src="<%= request.getContextPath()%>/refresh.png">
</div>
</div>
//here serveResource Method is used forhandle ajax call in jrs 286 portlet....
Now in java classs we implement serveResourse as well as validate the captch which user enter is valide or not..
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException, PortletException {
try {
com.liferay.portal.kernel.captcha.CaptchaUtil.serveImage(resourceRequest, resourceResponse);
} catch (Exception e) {
// TODO: handle exception
System.out.println("In captch");
}
}
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)throws IOException, PortletException
{
String enteredCaptchaText = ParamUtil.getString(actionRequest, "captchaText");
PortletSession session = actionRequest.getPortletSession();
String captchaText = (String)getCaptchaValueFromSession(session);
if (Validator.isNull(captchaText)) {
//actionRequest.setAttribute("AjexResponse", "<CodeValidation>Invalid<CodeValidation>");
actionResponse.setRenderParameter("captchaResult", "Invalid");
setValues(actionRequest,actionResponse);
}
//if (!StringUtils.equals(captchaText, enteredCaptchaText)) {
else if (!captchaText.equals(enteredCaptchaText)) {
//actionRequest.setAttribute("AjexResponse", "<CodeValidation>Invalid<CodeValidation>");
actionResponse.setRenderParameter("captchaResult", "Invalid");
setValues(actionRequest,actionResponse);
}
else{
System.out.println("Valide");
}
private String getCaptchaValueFromSession(PortletSession session) {
Enumeration<String> atNames = session.getAttributeNames();
while (atNames.hasMoreElements()) {
String name = atNames.nextElement();
if (name.contains("CAPTCHA_TEXT")) {
return (String) session.getAttribute(name);
}
}
return null;
}
}
Any query then comment or send email at chiragmsc007it@gmail.com
Here i want to explain how to add captch image in custom portlet & refresh that captch .So i explain step by step.
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
jQuery("#refreshCaptchaActivity").click(function()
{
jQuery(".captcha").attr("src", jQuery(".captcha").attr("src")+"&force=" + new Date().getMilliseconds());
return false;
});
</script>
<div>
<portlet:resourceURL var="captchaURL" />
<liferay-ui:captcha url="<%=captchaURL%>" />
<div style="padding: 23px 0 0 5px; cursor: pointer;">
<img id="refreshCaptchaActivity"
src="<%= request.getContextPath()%>/refresh.png">
</div>
</div>
//here serveResource Method is used forhandle ajax call in jrs 286 portlet....
Now in java classs we implement serveResourse as well as validate the captch which user enter is valide or not..
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException, PortletException {
try {
com.liferay.portal.kernel.captcha.CaptchaUtil.serveImage(resourceRequest, resourceResponse);
} catch (Exception e) {
// TODO: handle exception
System.out.println("In captch");
}
}
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)throws IOException, PortletException
{
String enteredCaptchaText = ParamUtil.getString(actionRequest, "captchaText");
PortletSession session = actionRequest.getPortletSession();
String captchaText = (String)getCaptchaValueFromSession(session);
if (Validator.isNull(captchaText)) {
//actionRequest.setAttribute("AjexResponse", "<CodeValidation>Invalid<CodeValidation>");
actionResponse.setRenderParameter("captchaResult", "Invalid");
setValues(actionRequest,actionResponse);
}
//if (!StringUtils.equals(captchaText, enteredCaptchaText)) {
else if (!captchaText.equals(enteredCaptchaText)) {
//actionRequest.setAttribute("AjexResponse", "<CodeValidation>Invalid<CodeValidation>");
actionResponse.setRenderParameter("captchaResult", "Invalid");
setValues(actionRequest,actionResponse);
}
else{
System.out.println("Valide");
}
private String getCaptchaValueFromSession(PortletSession session) {
Enumeration<String> atNames = session.getAttributeNames();
while (atNames.hasMoreElements()) {
String name = atNames.nextElement();
if (name.contains("CAPTCHA_TEXT")) {
return (String) session.getAttribute(name);
}
}
return null;
}
}
Any query then comment or send email at chiragmsc007it@gmail.com
grt!!!creation of captcha in custom template using ajax.its very informative and useful.thanks for sharing.keep blogging.
ReplyDeletecaptcha solver
wow!!!
ReplyDeletethe great blog.it is very informative and interesting.
keep blogging.
deathbycaptcha
Am using ur code for recaptcha, recaptcha is displaying but validations are not working. is there any configuration required for this.
ReplyDeleteI want to put this same functionality in my create_account.jsp hook then how can I fire my portlet action.. I am confused..
ReplyDelete