Enter your email address:

Delivered by FeedBurner


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
captcha project in jsp/servlet
12-19-2011, 05:44 PM
Post: #1
Exclamation captcha project in jsp/servlet
Already i wrote a post about creating random image using servlet that is loacted here In order to view links, you must have to reply to this thread., With the continuation here i give you complete code for

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>
<head>
<meta http-equiv="Content-Type" content="text/html" charset=UTF-8?>
<title>Captcha Project from In order to view links, you must have to reply to this thread.>/title>
</head>
<body>
<center>
<form method="post" action="checkCaptcha.do">
<table cellspacing="15?>
<tr>
<td align="center">
<img src="CaptchaServlet.do">
<br />
<br />
<input type="text" name="code">
</td>
</tr>
</table>
<input type="submit" value="submit">
</form>
<br>
<br>>/center>
</body>
</html>

create web.xml file for necessary servlet entry

Quote:<web-app>
<servlet>
<servlet-name>CaptchaServlet>/servlet-name>
<servlet-class>
CaptchaServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>CheckCaptcha>/servlet-name>
<servlet-class>
CheckCaptcha
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CaptchaServlet>/servlet-name>
<url-pattern>/CaptchaServlet.do>/url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CheckCaptcha>/servlet-name>
<url-pattern>/checkCaptcha.do>/url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp>/welcome-file>
</welcome-file-list>
</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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8?>
<title>Captcha>/title>
<link rel="stylesheet" href="theme/style.css">
</head>
<body>
<center>
<%=request.getAttribute("result")%>
</center>
</body>
</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.
Find all posts by this user
Add Thank You Quote this message in a reply
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Possibly Related Threads...
Thread: Author Replies Views: Last Post
Information JSP Complete Project download rajasri 25 2,887 05-18-2012 01:07 PM
Last Post: kusuma
Tongue Students management java project download rajasri 25 2,290 05-15-2012 12:45 PM
Last Post: itischinmay
  JSP Complete Project download admin 3 775 05-13-2012 09:23 PM
Last Post: viknass
Lightbulb free jsp project download rajasri 7 2,144 04-19-2012 12:25 PM
Last Post: poonam
Rainbow Simple Servlet Program mukesh1234 0 316 01-29-2012 08:02 PM
Last Post: mukesh1234
Tongue Creating captcha(random letter image) in jsp rajasri 0 546 12-19-2011 06:09 PM
Last Post: rajasri
Heart How to authenticate in a webapplication using jsp and servlet rajasri 0 803 12-13-2011 12:32 AM
Last Post: rajasri
Star Read data from xml and show in jsp page project download rajasri 0 950 12-12-2011 11:30 PM
Last Post: rajasri
Big Grin HTTP Status 404 - Servlet as is not available rajasri 0 889 12-12-2011 10:15 PM
Last Post: rajasri
Tongue How to run servlet in tomcat 5.5 rajasri 0 1,222 12-12-2011 10:01 PM
Last Post: rajasri

Forum Jump:


User(s) browsing this thread: 1 Guest(s)