Foren

Add captcha in my own portlet in liferay 6.1

thumbnail
behnaz eslami, geändert vor 11 Jahren.

Add captcha in my own portlet in liferay 6.1

Junior Member Beiträge: 54 Beitrittsdatum: 14.11.11 Neueste Beiträge
I add "captcha" in my own portlet based on this link "http://www.albertcoronado.com/2012/05/30/uso-de-captchas-en-liferay/" , but when I add my portlet in liferay I have some ERRORs that show EXCEPTIONS in console.

///////////////////////////////////////////////////// .java file /////////////////////////////////////////////////////

import java.io.IOException;
import java.util.Enumeration;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.util.ParamUtil;


public class test extends GenericPortlet {

/**
* Helper method to serve up the mandatory view mode.
*/
protected void doEdit(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletRequestDispatcher dispatcher = getPortletContext()
.getRequestDispatcher("/WEB-INF/jsp/test_edit.jsp");
dispatcher.include(request, response);
}

/**
* Helper method to serve up the mandatory view mode.
*/
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletRequestDispatcher dispatcher = getPortletContext()
.getRequestDispatcher("/WEB-INF/jsp/test_view.jsp");
dispatcher.include(request, response);
}

/**
* Called by the portlet container to allow the portlet to process an action request.
* This method is called if the client request was originated by a URL created
* (by the portlet) with the RenderResponse.createActionURL() method.
*/

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 {
PortletPreferences prefs = request.getPreferences();
String cmd = request.getParameter("cmd");
System.out.println(cmd);
System.out.println(request.getParameter("phoneNumber"));
System.out.println(request.getParameter("captchaText"));
response.setRenderParameter("phoneNumber", request.getParameter("phoneNumber"));
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 "";
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////// view.jsp /////////////////////////////////////////////////////
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>

<%@ taglib uri="/tld/liferay-ui.tld" prefix="liferay-ui" %>
<%@ taglib uri="/tld/aui.tld" prefix="aui" %>

<portlet:defineObjects />
<%--<%PortletPreferences prefs = renderRequest.getPreferences();%>--%>

<% PortletPreferences prefs = renderRequest.getPreferences(); %>

<p style="color:black">
test is in <b>view mode</b>
</p>
<liferay-ui:error key="Invalid-captcha" message="Invalid-captcha"></liferay-ui:error>
<form action="<portlet:actionURL/>" method="post">
<input type="hidden" name="<portlet:namespace/>cmd" value="postCode" />
<input type="text" name="captchaText" />
<br/>
<portlet:resourceURL var="captchaURL" />
<liferay-ui:captcha url="<%= captchaURL %>" />
<input type="submit" name = "submit" value="12345"/>
</form>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////// ERRORs in liferay console /////////////////////////////////////////////////////
06:31:35,720 ERROR [http-bio-8080-exec-51][IncludeTag:253] Current URL /web/guest/test?p_auth=4DQLTK
Pm&p_p_id=test_WAR_testPortlet_INSTANCE_yvzx45uZOU38&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&
p_p_col_id=column-1&p_p_col_count=1 generates exception: null
06:31:35,723 ERROR [http-bio-8080-exec-51][IncludeTag:154] java.lang.NullPointerException
at com.liferay.taglib.aui.InputTag.updateFormValidators(InputTag.java:270)
at com.liferay.taglib.aui.InputTag.doEndTag(InputTag.java:45)
at org.apache.jsp.html.taglib.ui.captcha.simplecaptcha_jsp._jspx_meth_aui_005finput_005f0(si
mplecaptcha_jsp.java:535)
at org.apache.jsp.html.taglib.ui.captcha.simplecaptcha_jsp._jspService(simplecaptcha_jsp.jav
a:467)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at com.liferay.portal.servlet.DirectRequestDispatcher.include(DirectRequestDispatcher.java:9
7)
at com.liferay.portal.servlet.PACLRequestDispatcherWrapper.doDispatch(PACLRequestDispatcherW
rapper.java:90)
at com.liferay.portal.servlet.PACLRequestDispatcherWrapper.include(PACLRequestDispatcherWrap
per.java:54)
at com.liferay.taglib.util.IncludeTag.include(IncludeTag.java:345)
at com.liferay.taglib.util.IncludeTag.doInclude(IncludeTag.java:245)
at com.liferay.taglib.util.IncludeTag.doEndTag(IncludeTag.java:86)
at org.apache.jsp.html.taglib.ui.captcha.page_jsp._jspService(page_jsp.java:428)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at com.liferay.portal.servlet.DirectRequestDispatcher.include(DirectRequestDispatcher.java:9
7)
at com.liferay.portal.servlet.PACLRequestDispatcherWrapper.doDispatch(PACLRequestDispatcherW
rapper.java:90)
at com.liferay.portal.servlet.PACLRequestDispatcherWrapper.include(PACLRequestDispatcherWrap
per.java:54)
at com.liferay.taglib.util.IncludeTag.include(IncludeTag.java:345)
at com.liferay.taglib.util.IncludeTag.doInclude(IncludeTag.java:245)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
thumbnail
Apoorva Prakash, geändert vor 11 Jahren.

RE: Add captcha in my own portlet in liferay 6.1

Liferay Master Beiträge: 658 Beitrittsdatum: 15.06.10 Neueste Beiträge
Hello Behzaz,

Have a look on this link.

Hope this will help...

Thanks and Regards,
Apoorva Prakash
thumbnail
Abhimanyu Saraswat, geändert vor 11 Jahren.

RE: Add captcha in my own portlet in liferay 6.1

Junior Member Beiträge: 62 Beitrittsdatum: 26.02.13 Neueste Beiträge
Hi Behnaz,

I am also getting the same exception. Could you please share the info about how did you solved this problem.

Thanks in Advance..

Thanks & Regards
Abhimayu Saraswat
Parikshit Sharma, geändert vor 11 Jahren.

RE: Add captcha in my own portlet in liferay 6.1

New Member Beiträge: 21 Beitrittsdatum: 16.01.13 Neueste Beiträge
behnaz eslami:
I add "captcha" in my own portlet based on this link "http://www.albertcoronado.com/2012/05/30/uso-de-captchas-en-liferay/" , but when I add my portlet in liferay I have some ERRORs that show EXCEPTIONS in console.

///////////////////////////////////////////////////// .java file /////////////////////////////////////////////////////

import java.io.IOException;
import java.util.Enumeration;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.util.ParamUtil;


public class test extends GenericPortlet {

/**
* Helper method to serve up the mandatory view mode.
*/
protected void doEdit(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletRequestDispatcher dispatcher = getPortletContext()
.getRequestDispatcher("/WEB-INF/jsp/test_edit.jsp");
dispatcher.include(request, response);
}

/**
* Helper method to serve up the mandatory view mode.
*/
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletRequestDispatcher dispatcher = getPortletContext()
.getRequestDispatcher("/WEB-INF/jsp/test_view.jsp");
dispatcher.include(request, response);
}

/**
* Called by the portlet container to allow the portlet to process an action request.
* This method is called if the client request was originated by a URL created
* (by the portlet) with the RenderResponse.createActionURL() method.
*/

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 {
PortletPreferences prefs = request.getPreferences();
String cmd = request.getParameter("cmd");
System.out.println(cmd);
System.out.println(request.getParameter("phoneNumber"));
System.out.println(request.getParameter("captchaText"));
response.setRenderParameter("phoneNumber", request.getParameter("phoneNumber"));
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 "";
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////// view.jsp /////////////////////////////////////////////////////
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>

<%@ taglib uri="/tld/liferay-ui.tld" prefix="liferay-ui" %>
<%@ taglib uri="/tld/aui.tld" prefix="aui" %>

<portlet:defineObjects />
<%--<%PortletPreferences prefs = renderRequest.getPreferences();%>--%>

<% PortletPreferences prefs = renderRequest.getPreferences(); %>

<p style="color:black">
test is in <b>view mode</b>
</p>
<liferay-ui:error key="Invalid-captcha" message="Invalid-captcha"></liferay-ui:error>
<form action="<portlet:actionURL/>" method="post">
<input type="hidden" name="<portlet:namespace/>cmd" value="postCode" />
<input type="text" name="captchaText" />
<br/>
<portlet:resourceURL var="captchaURL" />
<liferay-ui:captcha url="<%= captchaURL %>" />
<input type="submit" name = "submit" value="12345"/>
</form>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////// ERRORs in liferay console /////////////////////////////////////////////////////
06:31:35,720 ERROR [http-bio-8080-exec-51][IncludeTag:253] Current URL /web/guest/test?p_auth=4DQLTK
Pm&p_p_id=test_WAR_testPortlet_INSTANCE_yvzx45uZOU38&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&
p_p_col_id=column-1&p_p_col_count=1 generates exception: null
06:31:35,723 ERROR [http-bio-8080-exec-51][IncludeTag:154] java.lang.NullPointerException
at com.liferay.taglib.aui.InputTag.updateFormValidators(InputTag.java:270)
at com.liferay.taglib.aui.InputTag.doEndTag(InputTag.java:45)
at org.apache.jsp.html.taglib.ui.captcha.simplecaptcha_jsp._jspx_meth_aui_005finput_005f0(si
mplecaptcha_jsp.java:535)
at org.apache.jsp.html.taglib.ui.captcha.simplecaptcha_jsp._jspService(simplecaptcha_jsp.jav
a:467)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at com.liferay.portal.servlet.DirectRequestDispatcher.include(DirectRequestDispatcher.java:9
7)
at com.liferay.portal.servlet.PACLRequestDispatcherWrapper.doDispatch(PACLRequestDispatcherW
rapper.java:90)
at com.liferay.portal.servlet.PACLRequestDispatcherWrapper.include(PACLRequestDispatcherWrap
per.java:54)
at com.liferay.taglib.util.IncludeTag.include(IncludeTag.java:345)
at com.liferay.taglib.util.IncludeTag.doInclude(IncludeTag.java:245)
at com.liferay.taglib.util.IncludeTag.doEndTag(IncludeTag.java:86)
at org.apache.jsp.html.taglib.ui.captcha.page_jsp._jspService(page_jsp.java:428)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at com.liferay.portal.servlet.DirectRequestDispatcher.include(DirectRequestDispatcher.java:9
7)
at com.liferay.portal.servlet.PACLRequestDispatcherWrapper.doDispatch(PACLRequestDispatcherW
rapper.java:90)
at com.liferay.portal.servlet.PACLRequestDispatcherWrapper.include(PACLRequestDispatcherWrap
per.java:54)
at com.liferay.taglib.util.IncludeTag.include(IncludeTag.java:345)
at com.liferay.taglib.util.IncludeTag.doInclude(IncludeTag.java:245)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



You are getting error because the way you have included the tags (import statement are not correct),like to import liferay-ui we use
&lt;%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %&gt;

check /html/common/init.jsp file for import reference.