Creating Captcha Application
We can generate Captcha application in 2 modes
1.Online mode
2.Offline mode
1.Online mode
2.Offline mode
Online Mode:
Needs internet,we are going to use re-captcha api,which internally
uses db to store images and voices,it is widely used capcha in the
internet,like facebook etc.
re-captcha api is given by Google.
Needs internet,we are going to use re-captcha api,which internally
uses db to store images and voices,it is widely used capcha in the
internet,like facebook etc.
re-captcha api is given by Google.
Offline mode:
We are going to use some other offline apis like
It generates captcha but it is one per session,
so if you refresh the page,it wont give you new captcha
We are going to use some other offline apis like
It generates captcha but it is one per session,
so if you refresh the page,it wont give you new captcha
In this article we will see both procedures i.e Online Mode & Offline Mode.
1. Online Captcha
MyOnlineCapcha
|
|—->WEB-INF
| |——>web.xml
| |——>lib
| |—–>recaptcha4j-0.0.7.jar
|——>index.jsp
|——>process.jsp
Creating Capcha in your Application using google Re-Captcha
|
|—->WEB-INF
| |——>web.xml
| |——>lib
| |—–>recaptcha4j-0.0.7.jar
|——>index.jsp
|——>process.jsp
Creating Capcha in your Application using google Re-Captcha
1.Open the below link and Click get re-CAPTCHA Link
2. Click on “Sign up Now!” button and signup to Your gmail Account inorder to use Re-Captcha.
3. Create a reCAPTCHA Key.Enter a domain(can be anything,non-existing as well),Select
Enable this key on all domains.Otherwise the capcha will work only on your domain,if
you want to use them in all other domains you must select them as well.
Enable this key on all domains.Otherwise the capcha will work only on your domain,if
you want to use them in all other domains you must select them as well.
Click on create Key.
4. The above step will generate private and public key for captcha.
5. We are now going to use those private and pubic key in our java application inorder to
generate captcha.Download the re-Captcha library from the below link
generate captcha.Download the re-Captcha library from the below link
6. Create a New web Project.Include the jars into the project
To show the image Include the following code in index.jsp
index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| <%@ page import="net.tanesha.recaptcha.ReCaptcha"%><%@ page import="net.tanesha.recaptcha.ReCaptchaFactory"%><html><body> <form action="process.jsp" method="post"> <% //ReCaptcha c = ReCaptchaFactory.newReCaptcha("your_public_key", "your_private_key", false); ReCaptcha captcha = ReCaptchaFactory.newReCaptcha("6LfwU-kSAAAAANon6jhYZbfeeH-vgCKncnzs9WOr", "6LfwU-kSAAAAAGiis8gP7kS42ty9Wt_1euOGzj3J", false); out.print(captcha.createRecaptchaHtml(null, null)); %> <input type="submit" value="submit" /> </form></body></html> |
7. To Process the answer create process.jsp
process.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| <%@ page import="net.tanesha.recaptcha.ReCaptchaImpl"%><%@ page import="net.tanesha.recaptcha.ReCaptchaResponse"%><html><body> <% String remoteAddr = request.getRemoteAddr(); ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPrivateKey("6LfwU-kSAAAAAGiis8gP7kS42ty9Wt_1euOGzj3J"); String challenge = request.getParameter("recaptcha_challenge_field"); String uresponse = request.getParameter("recaptcha_response_field"); ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse); if (reCaptchaResponse.isValid()) { out.print("Answer was entered correctly!"); } else { out.print("Answer is wrong"); } %></body></html> |
2. Offline Captcha
Directory Structure
SimpleCaptchaDemo
|
|——–>WEB-INF
| |——>web.xml
| |——>lib
| |—–>simplecaptcha-1.2-jdk1.5.jar
|–>index.jsp
|–>process.jsp
|
|——–>WEB-INF
| |——>web.xml
| |——>lib
| |—–>simplecaptcha-1.2-jdk1.5.jar
|–>index.jsp
|–>process.jsp
1. Create web.xml
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
| <web-app> <servlet> <servlet-name>demo</servlet-name> <servlet-class> nl.captcha.servlet.StickyCaptchaServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>demo</servlet-name> <url-pattern>/stickyImage</url-pattern> </servlet-mapping></web-app> |
1
2
3
4
5
| <img src="stickyImage" /><form action="process.jsp">Your Answer<input type="text" name="answer"><input type="submit" name="submit"/></form> |
1
2
3
4
5
6
7
8
9
10
11
12
13
| <%@page import="nl.captcha.Captcha"%><% Captcha captcha=(Captcha)session.getAttribute(Captcha.NAME); request.setCharacterEncoding("UTF-8"); String answer=request.getParameter("answer"); if(captcha.isCorrect(answer)) out.println("Correct"); else out.println("Incorrect");%> |
No comments:
Post a Comment