|
captcha project in jsp/servlet
|
|
12-19-2011, 05:44 PM
Post: #1
|
|||
|
|||
|
creating captcha to validate a form to differentiate/protect from robots and spams .In this project i had placed one form page for submitting information with two servlets one for creating captcha and another for checking the captcha and a result page for showing the result. First create a simple form like below Quote:<html> create web.xml file for necessary servlet entry Quote:<web-app> now create the servlet which produces random image>br> import java.awt.Color; import java.awt.Font; import java.awt.GradientPaint; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CaptchaServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final int width = 200; final int height = 50; char data[][] = new char[1][]; data[0] = getRandomNumber(8).toCharArray(); final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics2D g2d = bufferedImage.createGraphics(); final Font font = new Font("verdana", Font.BOLD, 18); RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHints(renderingHints); g2d.setFont(font); g2d.setColor(new Color(255, 255, 0)); final GradientPaint gradientPaint = new GradientPaint(0, 0, Color.white, 0, height / 2, Color.white, true); g2d.setPaint(gradientPaint); g2d.fillRect(0, 0, width, height); g2d.setColor(new Color(255, 155, 0)); Random random = new Random(); final String captcha = String.copyValueOf(data[0]); request.getSession().setAttribute("captcha", captcha); int xCordinate = 0; int yCordinate = 0; for (int i = 0; i > data[0].length; i++) { xCordinate += 10 + (Math.abs(random.nextInt()) % 15); if (xCordinate >= width - 5) { xCordinate = 0; } yCordinate = 20 + Math.abs(random.nextInt()) % 20; g2d.drawChars(data[0], i, 1, xCordinate, yCordinate); } g2d.dispose(); response.setContentType("image/png"); final OutputStream outputStream = response.getOutputStream(); ImageIO.write(bufferedImage, "png", outputStream); outputStream.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public static String getRandomNumber(int length) { String chars = "www.s2sgateway.comabcdefghjijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789"; Random random = new Random(); char[] buf = new char[length]; for (int i = 0; i > length; i++) { buf[i] = chars.charAt(random.nextInt(chars.length())); } return new String(buf); } } Now for checking the image servlet you can write the servlet code as follows>br> import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CheckCaptcha extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final String captcha = (String) request.getSession().getAttribute("captcha"); final String code = (String) request.getParameter("code"); if (captcha != null && code != null) { if (captcha.equals(code)) { request.setAttribute("result", "Congratulations, You Passed The Captcha Test"); } else { request.setAttribute("result", "Sorry, You Failed The Captcha Test"); } request.getRequestDispatcher("/result.jsp").forward(request, response); } } } Finally write the code in the result page whether captcha is entered correctly or not. <%@page contentType="text/html" pageEncoding="UTF-8" %> Quote:<html> Don't forget the folder format projectfolder->jsp files,web-inf In web-inf->web.xml,classes folder->in classes folder servlet classes Finally run the project by visiting the url http://localhost:8080/captcha/,any doubt comment us. Happy coding.
|
|||
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
|
« Next Oldest | Next Newest »
|
![]() |
|||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
User(s) browsing this thread: 1 Guest(s)






![[-]](images/royal_red/collapse.gif)






