Using Captcha in portlet:


In view.jsp:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib prefix="liferay-ui" uri="http://liferay.com/tld/ui" %>
<portlet:defineObjects />

<portlet:resourceURL var="captchaURL"/>

<form action="<portlet:actionURL/>" method="post">
      <liferay-ui:captcha url="<%= captchaURL %>"/>
      <input type="submit" value="verify"></input>
</form>

In controller class:
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {
    try {
        com.liferay.portal.kernel.captcha.CaptchaUtil.serveImage(resourceRequest, resourceResponse);
    } catch (Exception e) {    e.printStackTrace(); }
    }
public synchronized void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
        String eText = ParamUtil.getString(request, "captchaText");
        String cText = getCaptchaText( request );

        if ( eText.equals( cText ) ) {
               /* Captcha OK */
               } else {
               /* Captcha KO */
        }
    }
private String getCaptchaText( ActionRequest request ) {
    PortletSession session = request.getPortletSession();
    Enumeration<String> atNames = session.getAttributeNames();
    while (atNames.hasMoreElements()) {
        String name = atNames.nextElement();
        if (name.contains("CAPTCHA_TEXT")) {
                       String cText=(String) session.getAttribute(name);
                       return cText;
               }
        }

    return "";

    }

Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.