<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>s2sgateway &#187; captcha creation</title>
	<atom:link href="http://s2sgateway.com/category/captcha-creation/feed/" rel="self" type="application/rss+xml" />
	<link>http://s2sgateway.com</link>
	<description>Practical Learning of Programming languages such us j2ee,java,jsp</description>
	<lastBuildDate>Sat, 21 Jan 2012 18:55:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>captcha project in jsp/servlet</title>
		<link>http://s2sgateway.com/jsp/captcha-project-in-jspservlet/</link>
		<comments>http://s2sgateway.com/jsp/captcha-project-in-jspservlet/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 14:06:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[captcha creation]]></category>
		<category><![CDATA[J2EE codes]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Jsp]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[capcha project in jsp]]></category>
		<category><![CDATA[captcha full code]]></category>
		<category><![CDATA[captcha in jsp form]]></category>
		<category><![CDATA[captcha using servlet]]></category>
		<category><![CDATA[create capcha in jsp]]></category>
		<category><![CDATA[create captcha using j2ee]]></category>
		<category><![CDATA[create random image using jsp]]></category>
		<category><![CDATA[how to create a captcha in jsp]]></category>
		<category><![CDATA[how to create random image using servlet]]></category>
		<category><![CDATA[how to use catcha in forms]]></category>
		<category><![CDATA[pagination in jsp]]></category>
		<category><![CDATA[stop spam using captcha]]></category>

		<guid isPermaLink="false">http://s2sgateway.com/?p=407</guid>
		<description><![CDATA[Already i wrote a post about creating random image using servlet that is loacted here http://s2sgateway.com/?p=378,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 [...]<p><a href="http://s2sgateway.com/jsp/captcha-project-in-jspservlet/">captcha project in jsp/servlet</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Already i wrote a post about creating random image using servlet that is loacted here <a href="http://s2sgateway.com/?p=378">http://s2sgateway.com/?p=378</a>,With the continuation here i give you complete code for<br />
<h3>creating captcha to validate a form to differentiate/protect from robots and spams</h3>
<p>.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.</h3>
<p>.<br />
<script type="text/javascript">
<!--
google_ad_client = "pub-0609065187474065";
/* post link adds for s2s */
google_ad_slot = "4731489867";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</br></p>
<p>First create a simple form like below</p>
<div style="margin-bottom:2px">
<pre style="margin:0px;padding:6px;border:1px inset;width:420px;max-width:420px;height:120px;text-align:left;overflow:auto"><code>
<pre>
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html" charset=UTF-8?&gt;
&lt;title&gt;Captcha Project from www.s2sgateway.com&gt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;form method="post" action="checkCaptcha.do"&gt;
&lt;table cellspacing="15?&gt;
&lt;tr&gt;
&lt;td align="center"&gt;
&lt;img src="CaptchaServlet.do"&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;input type="text" name="code"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input type="submit" value="submit"&gt;
&lt;/form&gt;
&lt;br&gt;
&lt;br&gt;&gt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p></code></pre>
</div>
<p>create web.xml file for necessary servlet entry</p>
<div style="margin-bottom:2px">
<pre style="margin:0px;padding:6px;border:1px inset;width:420px;max-width:420px;height:120px;text-align:left;overflow:auto"><code>
<pre>
&lt;web-app&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;CaptchaServlet&gt;/servlet-name&gt;
&lt;servlet-class&gt;
CaptchaServlet
&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;CheckCaptcha&gt;/servlet-name&gt;
&lt;servlet-class&gt;
CheckCaptcha
&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;CaptchaServlet&gt;/servlet-name&gt;
&lt;url-pattern&gt;/CaptchaServlet.do&gt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;CheckCaptcha&gt;/servlet-name&gt;
&lt;url-pattern&gt;/checkCaptcha.do&gt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;welcome-file-list&gt;
&lt;welcome-file&gt;index.jsp&gt;/welcome-file&gt;
&lt;/welcome-file-list&gt;
&lt;/web-app&gt;
</pre>
<p></code></pre>
</div>
<p>now create the servlet which produces random image&gt;br&gt;</p>
<div style="margin-bottom:2px">
<pre style="margin:0px;padding:6px;border:1px inset;width:420px;max-width:420px;height:120px;text-align:left;overflow:auto"><code>
<pre>
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 &gt; data[0].length; i++) {
xCordinate += 10 + (Math.abs(random.nextInt()) % 15);
if (xCordinate &gt;= 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 &gt; length; i++) {
buf[i] = chars.charAt(random.nextInt(chars.length()));
}
return new String(buf);
}
}
</pre>
<p></code></pre>
</div>
<p>Now for checking the image servlet you can write the servlet code as follows&gt;br&gt;</p>
<div style="margin-bottom:2px">
<pre style="margin:0px;padding:6px;border:1px inset;width:420px;max-width:420px;height:120px;text-align:left;overflow:auto"><code>
<pre>

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 &#038;&#038; 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);
}
}
}
</pre>
<p></code></pre>
</div>
<p>Finally write the code in the result page whether captcha is entered correctly or not.</p>
<div style="margin-bottom:2px">
<pre style="margin:0px;padding:6px;border:1px inset;width:420px;max-width:420px;height:120px;text-align:left;overflow:auto"><code>
<pre>
&lt;%@page contentType="text/html" pageEncoding="UTF-8" %&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8?&gt;
&lt;title&gt;Captcha&gt;/title&gt;
&lt;link rel="stylesheet" href="theme/style.css"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;%=request.getAttribute("result")%&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p></code></pre>
</div>
<p>
Don&#8217;t forget the folder format projectfolder->jsp files,web-inf<br />
In web-inf->web.xml,classes folder->in classes folder servlet classes<br />
Finally run the project by visiting the url http://localhost:8080/captcha/,any doubt comment us happy coding.</p>
<p><a href="http://s2sgateway.com/jsp/captcha-project-in-jspservlet/">captcha project in jsp/servlet</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></content:encoded>
			<wfw:commentRss>http://s2sgateway.com/jsp/captcha-project-in-jspservlet/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating captcha(random letter image) in jsp</title>
		<link>http://s2sgateway.com/jsp/creating-captcharandom-letter-image-in-jsp/</link>
		<comments>http://s2sgateway.com/jsp/creating-captcharandom-letter-image-in-jsp/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 14:07:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[captcha creation]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Jsp]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[automatic captcha creation]]></category>
		<category><![CDATA[captcha creation using servlet]]></category>
		<category><![CDATA[captcha creator free download]]></category>
		<category><![CDATA[captcha image using servlet]]></category>
		<category><![CDATA[captcha in jsp form]]></category>
		<category><![CDATA[captcha using servlet]]></category>
		<category><![CDATA[create captcha easily]]></category>
		<category><![CDATA[how to create a captcha in java]]></category>
		<category><![CDATA[how to create a captcha in jsp]]></category>
		<category><![CDATA[random letters in servlet]]></category>
		<category><![CDATA[stop spam using captcha]]></category>

		<guid isPermaLink="false">http://s2sgateway.com/?p=378</guid>
		<description><![CDATA[Today we are going to learn how to create a random letter image known as captcha using servlet. Just a simple coding can do it copy the code below and save it and compile it you can directly view the servlet in tomcat using url &#8220;http://localhost:8080/captcha/servlet/CaptchaServlet&#8221; for this make adjustment in web.xml file by adding [...]<p><a href="http://s2sgateway.com/jsp/creating-captcharandom-letter-image-in-jsp/">Creating captcha(random letter image) in jsp</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today we are going to learn how to create a random letter image known as captcha using servlet.<br />
Just a simple coding can do it copy the code below and save it and compile it you can directly view the servlet in tomcat using url &#8220;http://localhost:8080/captcha/servlet/CaptchaServlet&#8221; for this make adjustment in web.xml file by adding servlet and servletmapping entries.</p>
<div style="margin-bottom:2px">
<pre style="margin:0px;padding:6px;border:1px inset;width:490px;max-width:490px;height:220px;text-align:left;overflow:auto"><code>
<pre>
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);

}

}
</pre>
<p></code></pre>
</div>
<p>
that's it you have created captcha image using servlet,Here you can give any letters or numbers in String chars = "";assignment values.<br />
Change the color by changing this code "final GradientPaint gradientPaint = new GradientPaint(0, 0, Color.white, 0, height / 2,<br />
Color.white, true);"Here i used this image as png because it is good for web developement, you can try others as well like jpg or gif do comment us<br />any doubt ask us Happy coding.</p>
<p><a href="http://s2sgateway.com/jsp/creating-captcharandom-letter-image-in-jsp/">Creating captcha(random letter image) in jsp</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></content:encoded>
			<wfw:commentRss>http://s2sgateway.com/jsp/creating-captcharandom-letter-image-in-jsp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>


