Sunday, September 15, 2013
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart"), a trademark of Carnegie Mellon University, is a type of challenge-response test used in computing to determine whether or not the user is human. This requires the user to type the letters of a distorted image, sometimes with the addition of an obscured sequence of letters or digits that appears on the screen. Because the test is administered by a computer, in contrast to the standard Turing test that is administered by a human, a CAPTCHA is sometimes described as a reverse Turing test. This test is most common when someone registers or opens a new account in any website.
Here we will be using jcaptcha library for captcha generation and validation. In our demo we will follow MVC architecture to carry this out. We have the following classes, servlets and JSPs in our project.
-------------------------------------------------------------------------------------------------------------------------
/* Code for login.jsp */
/* Code for CaptchaService.java */
/* Code for CaptchaGeneratorServlet.java */
/* Code for CaptchaVerifierServlet.java */
Here we will be using jcaptcha library for captcha generation and validation. In our demo we will follow MVC architecture to carry this out. We have the following classes, servlets and JSPs in our project.
- login.jsp : This is the home page. Here the captcha challenge image will be shown with a text-box to enter the characters. On submitting it is validated by a servlet.
- CaptchaService class : This is a singleton class that is responsible for generating the instance that will help in generating the challenge image.
- CaptchaGeneratorServlet class : this servlet has a GET method which generates the image with CaptchaService class and converts it to a byte array and then writes to the output stream. The image is generated using the session id of the user which is used as the captcha id.
- CaptchaVerifierServlet class : It has a POST method which receives the input characters from usere and validates it. The result is returned as a boolean which is set as a session attribute and for the result to be shown it is forwarded to a JSP.
- results.jsp : It extracts the session attribute and displays result according to that.
-------------------------------------------------------------------------------------------------------------------------
Java Source Code
-------------------------------------------------------------------------------------------------------------------------/* Code for login.jsp */
<!DOCTYPE html> <html> <head> <title>JCaptcha Demo</title> </head> <body> <h2>JCaptcha Demo</h2> <form action="captcha-verifier" method="post"> <input type="hidden" name="captchaID" value="<%= session.getId() %>"/> <table><tr> <td valign="middle">Enter these letters:<br/> <img src="./captcha-generator" align="middle" alt="Enter the characters appearing in this image" border="1"/></td> <td><input type="text" name="inChars"/></td> </tr> <tr> <td><input type="submit" value="Submit"/></td> </tr> </table> </form> </body> </html>
/* Code for CaptchaService.java */
package java_code_house.jcaptcha; import com.octo.captcha.service.image.ImageCaptchaService; import com.octo.captcha.service.image.DefaultManageableImageCaptchaService; public class CaptchaService{ // a singleton class private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService(); public static ImageCaptchaService getInstance(){ return instance; } }
/* Code for CaptchaGeneratorServlet.java */
package java_code_house.jcaptcha; import com.octo.captcha.service.CaptchaServiceException; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/captcha-generator") public class CaptchaGeneratorServlet extends HttpServlet{ private static final long serialVersionUID=1L; protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { ByteArrayOutputStream imgStream = new ByteArrayOutputStream(); byte[] captchaBytes=null; try{ // Session ID is used to identify the particular captcha String captchaId = request.getSession().getId(); // Generate the captcha image BufferedImage img = CaptchaService.getInstance().getImageChallengeForID( captchaId, request.getLocale() ); ImageIO.write(img, "jpeg", imgStream ); captchaBytes = imgStream.toByteArray(); // Clear any existing flag request.getSession().removeAttribute("result"); }catch( CaptchaServiceException|IOException ex ) { System.out.println(ex); } // Set appropriate http headers response.setHeader( "Cache-Control", "no-store" ); response.setHeader( "Pragma", "no-cache" ); response.setDateHeader( "Expires", 0 ); response.setContentType( "image/jpeg"); // Write the image to the client OutputStream os = response.getOutputStream(); os.write(captchaBytes); os.flush(); os.close(); } }
/* Code for CaptchaVerifierServlet.java */
package java_code_house.jcaptcha; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.octo.captcha.service.CaptchaServiceException; @WebServlet("/captcha-verifier") public class CaptchaVerifierServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the request params String cId = request.getParameter("captchaID"); String inChars = request.getParameter("inChars"); // Validate whether input from user is correct boolean hasPassed = validateCaptcha(cId, inChars ); // Set flag into session request.getSession().setAttribute( "result", new Boolean(hasPassed) ); // Forward request to results page request.getRequestDispatcher( "/results.jsp" ).forward( request, response ); } private boolean validateCaptcha( String captchaId, String inputChars ){ boolean b = false; try{ b = CaptchaService.getInstance().validateResponseForID( captchaId, inputChars ); }catch( CaptchaServiceException cse ){} return b; } }
<!DOCTYPE html>
<html>
<head>
<title>JCaptcha Demo - Result</title>
</head>
<body>
<% Boolean b = (Boolean)session.getAttribute("result");
if ( b!=null && b.booleanValue() ){
%>
Congrats! You passed the Captcha test!
<% } else { %>
Sorry, you failed the Captcha test.
<% } %>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
Download Links
-------------------------------------------------------------------------------------------------------------------------
NOTE : While exporting the project from eclipse, optimization for Apache Tomcat v7 was done. Also it includes the source files as well as the jar files required to execute this.
Happy coding :)
Labels:J2EE
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
ARE YOU SURE IT WORK?
ReplyDeleteYes it does work
DeleteThe image doesn't load.
ReplyDeleteIt might be due to some other reasons. The code is fine and tested before posting. Try downloading the WAR from link given in post and test
DeleteFile not found in those link
DeleteHow to execute it?
ReplyDeleteOh men, thank so much, you helped me alot
ReplyDeleteand how can we refresh the image
ReplyDelete