<?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; Servlet</title>
	<atom:link href="http://s2sgateway.com/category/servlet/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>Servlet example for printing random values</title>
		<link>http://s2sgateway.com/servlet/servlet-example-for-printing-random-values/</link>
		<comments>http://s2sgateway.com/servlet/servlet-example-for-printing-random-values/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 13:27:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[printing values using servlet]]></category>
		<category><![CDATA[random values in jsp]]></category>
		<category><![CDATA[Servlet example for printing random values]]></category>
		<category><![CDATA[servlet tutorials]]></category>
		<category><![CDATA[simple struts example]]></category>
		<category><![CDATA[struts projects download]]></category>
		<category><![CDATA[struts tutorials]]></category>

		<guid isPermaLink="false">http://s2sgateway.com/?p=711</guid>
		<description><![CDATA[•Servlets are Java programming language objects that dynamically process requests and construct responses.Here you are going to learn Servlet example for printing random values with Explanation, First write one serlvet program Step1: To run servlet You need to compile it first for that you need any of the Jar files given below 1.servlet-api.jar (included in [...]<p><a href="http://s2sgateway.com/servlet/servlet-example-for-printing-random-values/">Servlet example for printing random values</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></description>
			<content:encoded><![CDATA[<p>•Servlets are Java programming language objects that dynamically process requests and construct responses.Here you are going to learn Servlet example for printing random values with Explanation, First write one serlvet program</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class colorservlet extends HttpServlet
{
public void doGet(HttpServletRequest
request,HttpServletResponse response)throws ServletException,IOException
{
String color=request.getParameter(”color”);
response.setContentType(”text/html”);
PrintWriter pw=response.getWriter();
pw.println(”
&lt;div&gt;
&lt;div&gt;&lt;strong&gt;The selected color is:”);
pw.println(color);
pw.close();
}
}
</pre>
<p>Step1:<br />
To run servlet You need to compile it first for that you need any of the Jar files given below<br />
1.servlet-api.jar (included in tomcat/lib) <a href=”http://tomcat.apache.org/download-55.cgi”>Download Tomcat</a><br />
or<br />
2.Jsdk.jar(included in jsdk2.0 or later)<a href=”http://java.sun.com/products/servlet/<br />
download.html”>Download from sun site</a><br />
After downloading installed it and give classpath for example if you are installing it in the c:/tomcat 5.5/”<br />
then you had to assign classpath as<br />
set classpath=c:/tomcat5.5/common/lib/servlet-api.jar;and now compile your program asusual like javac<br />
colorservlet.java.<br />
Step2:<br />
To run serlvet you need to create a web.xml file<br />
sample web.xml file for helloworld</p>
<pre class="brush: xml; title: ; notranslate">
&lt;web-app&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;colorservlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;colorservlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;colorservlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/colorservlet&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;welcome-file-list&gt;
&lt;welcome-file&gt;colorservlet.html&lt;/welcome-file&gt;
&lt;/welcome-file-list&gt;
&lt;/web-app&gt;
</pre>
<p>Now create colorservlet.html</p>
<pre class="brush: css; html-script: true; title: ; notranslate">
&lt;html&gt;
&lt;body&gt;
&lt;center&gt;
&lt;form name=”form1″ method=”get”
action=”colorservlet”&gt;
&lt;b&gt;Color:&lt;/b&gt;
&lt;select name=”color” size=”1″&gt;
&lt;option value=”red”&gt;Red&lt;/option&gt;
&lt;option value=”green”&gt;Green&lt;/option&gt;
&lt;option value=”yellow”&gt;Yellow&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;&lt;br&gt;
&lt;input type=submit value=”submit”&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Your folder structure will look below<br />
colorservlet folder(inside webapps of tomcat installation folder)inside coloservlet folder it look like below.<br />
=>WEB-INF=>classes=>class files of servlet<br />
=>lib(jar files servlet-api.jar)<br />
=>web.xml<br />
=>colorservlet.html<br />
Step3 :<br />
goto</p>
<p>http://localhost:8080/colorservlet/</p>
<p>and run the program<br />
Happy coding</p>
<p><a href="http://s2sgateway.com/servlet/servlet-example-for-printing-random-values/">Servlet example for printing random values</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></content:encoded>
			<wfw:commentRss>http://s2sgateway.com/servlet/servlet-example-for-printing-random-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to authenticate in a webapplication using jsp and servlet</title>
		<link>http://s2sgateway.com/jsp/how-to-authenticate-in-a-webapplication-using-jsp-and-servlet/</link>
		<comments>http://s2sgateway.com/jsp/how-to-authenticate-in-a-webapplication-using-jsp-and-servlet/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 09:13:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Jsp]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[authentication in jsp]]></category>
		<category><![CDATA[authentication using jsp and servlet]]></category>
		<category><![CDATA[checking null values in javascript]]></category>
		<category><![CDATA[checking username and password in java]]></category>
		<category><![CDATA[checking username and password in jsp and servlet]]></category>
		<category><![CDATA[login form in j2ee]]></category>
		<category><![CDATA[login form using java]]></category>
		<category><![CDATA[login form using jsp]]></category>
		<category><![CDATA[login form using struts]]></category>
		<category><![CDATA[servlet authentication example]]></category>
		<category><![CDATA[simple login form in java]]></category>

		<guid isPermaLink="false">http://s2sgateway.com/?p=512</guid>
		<description><![CDATA[How to authenticate in a webapplication using jsp and servlet is a post from: s2sgateway<p><a href="http://s2sgateway.com/jsp/how-to-authenticate-in-a-webapplication-using-jsp-and-servlet/">How to authenticate in a webapplication using jsp and servlet</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></description>
			<content:encoded><![CDATA[How to authenticate in a webapplication using jsp and servlet is a post from: s2sgateway<p><a href="http://s2sgateway.com/jsp/how-to-authenticate-in-a-webapplication-using-jsp-and-servlet/">How to authenticate in a webapplication using jsp and servlet</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></content:encoded>
			<wfw:commentRss>http://s2sgateway.com/jsp/how-to-authenticate-in-a-webapplication-using-jsp-and-servlet/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to extract war file in java</title>
		<link>http://s2sgateway.com/jsp/how-to-extract-war-file-in-java/</link>
		<comments>http://s2sgateway.com/jsp/how-to-extract-war-file-in-java/#comments</comments>
		<pubDate>Mon, 31 May 2010 12:02:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Jsp]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[creating web archive files]]></category>
		<category><![CDATA[deploying webarchive files]]></category>
		<category><![CDATA[extract war file in java]]></category>
		<category><![CDATA[extract war file in jsp]]></category>
		<category><![CDATA[how to extract a war file]]></category>

		<guid isPermaLink="false">http://s2sgateway.com/?p=447</guid>
		<description><![CDATA[How to extract war file in java is a post from: s2sgateway<p><a href="http://s2sgateway.com/jsp/how-to-extract-war-file-in-java/">How to extract war file in java</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></description>
			<content:encoded><![CDATA[How to extract war file in java is a post from: s2sgateway<p><a href="http://s2sgateway.com/jsp/how-to-extract-war-file-in-java/">How to extract war file in java</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></content:encoded>
			<wfw:commentRss>http://s2sgateway.com/jsp/how-to-extract-war-file-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>How to run servlet in tomcat 5.5</title>
		<link>http://s2sgateway.com/servlet/how-to-run-servlet-in-tomcat-5-5/</link>
		<comments>http://s2sgateway.com/servlet/how-to-run-servlet-in-tomcat-5-5/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 13:49:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[how to run your first servlet]]></category>
		<category><![CDATA[run servlet in tomcat 5.5]]></category>
		<category><![CDATA[servlet errors]]></category>
		<category><![CDATA[servlet example]]></category>

		<guid isPermaLink="false">http://s2sgateway.com/?p=331</guid>
		<description><![CDATA[How to run servlet in tomcat 5.5 is a post from: s2sgateway<p><a href="http://s2sgateway.com/servlet/how-to-run-servlet-in-tomcat-5-5/">How to run servlet in tomcat 5.5</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></description>
			<content:encoded><![CDATA[How to run servlet in tomcat 5.5 is a post from: s2sgateway<p><a href="http://s2sgateway.com/servlet/how-to-run-servlet-in-tomcat-5-5/">How to run servlet in tomcat 5.5</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></content:encoded>
			<wfw:commentRss>http://s2sgateway.com/servlet/how-to-run-servlet-in-tomcat-5-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTTP Status 404 &#8211; Servlet as is not available</title>
		<link>http://s2sgateway.com/servlet/http-status-404-servlet-as-is-not-available/</link>
		<comments>http://s2sgateway.com/servlet/http-status-404-servlet-as-is-not-available/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 07:35:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[creating web.xml file]]></category>
		<category><![CDATA[error during running servlet]]></category>
		<category><![CDATA[how to run your first servlet]]></category>
		<category><![CDATA[HTTP Status 404 - Servlet as is not available]]></category>
		<category><![CDATA[servlet errors]]></category>

		<guid isPermaLink="false">http://s2sgateway.com/?p=328</guid>
		<description><![CDATA[HTTP Status 404 &#8211; Servlet as is not available is a post from: s2sgateway<p><a href="http://s2sgateway.com/servlet/http-status-404-servlet-as-is-not-available/">HTTP Status 404 &#8211; Servlet as is not available</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></description>
			<content:encoded><![CDATA[HTTP Status 404 &#8211; Servlet as is not available is a post from: s2sgateway<p><a href="http://s2sgateway.com/servlet/http-status-404-servlet-as-is-not-available/">HTTP Status 404 &#8211; Servlet as is not available</a> is a post from: <a href="http://s2sgateway.com">s2sgateway</a></p>
]]></content:encoded>
			<wfw:commentRss>http://s2sgateway.com/servlet/http-status-404-servlet-as-is-not-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>


