servlet一次性验证码书写步骤

首先,我们单单来写在网页中生成验证码图片的servlet

GetImageServlet

public class GetImageServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//get image
		
		//create image in memory
		int width = 80;
		int height = 40;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		
		//create stack of image 
		Graphics graphics = image.getGraphics();
		//make sure the color of the pen
		graphics.setColor(Color.BLACK);
		//draw the rect
		graphics.fillRect(0, 0, width, height);
		
		graphics.setColor(Color.WHITE);
		//draw the rect
		graphics.fillRect(1, 1, width-2, height-2);
		//input the number
		String data="qwertyuioplkjhgfdsazxcvbnmPOIUYTREWQLKJHGFDSAMNBVCXZZZ1234567890";
		graphics.setFont(new Font("宋体",Font.BOLD,30));
		Random random = new Random();
		for (int i = 0; i < 4; i++) {
			graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
			int index = random.nextInt(62);
			String str = data.substring(index,index+1);
			graphics.drawString(str, 20*i, 30);
		}
<span style="white-space:pre">		</span>//make some fringe in image
		for (int i = 0; i < 10; i++) {
			graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
			graphics.drawLine(random.nextInt(width), random.nextInt(height),  random.nextInt(width),random.nextInt(height));
		}
		//point the pic
		response.setContentType("image/jpeg");
		//send the pic to browse
		ImageIO.write(image, "jpg", response.getOutputStream());
		
	}

}
 写完这个后,在web.xml中配置还了以后,我们在网页中写<img alt="" src="getImageServlet">便可得到随机生成的验证码!!


然后我们需要获得网页中用户输入的验证吗,并且和生成的验证码进行对比

新建另一个GetNumberservle 来进行匹配


String num1 = request.getParameter("用户输入的验证吗");
HttpSessin  session = request.getsession();
String num2 = (String)sesion.getAttribute("number",buffer);
//and then you can <span id="blng_part_tran_2_1" data-aligning="#blng_part_src_2_1,#blng_part_tran_2_1" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; background-color: rgb(242, 242, 242); ">have</span><span style="color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; background-color: rgb(242, 242, 242); "> </span><span id="blng_part_tran_2_2" data-aligning="#blng_part_src_2_6,#blng_part_tran_2_2" class="highLight" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; background-color: rgb(189, 213, 238); color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; ">some comparison</span><span style="color: rgb(67, 67, 67); font-family: Tahoma, Arial; line-height: 24px; background-color: rgb(242, 242, 242); "> .</span>


在这之前我们需要在之前的getImageServlet 添加一个功能

就是在GetImageServlet中把随机生成的验证码放到session 中


StringBuffer buffer= new StringBuffer();
<pre name="code" class="java">		for (int i = 0; i < 4; i++) {
			graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
			int index = random.nextInt(62);
			String str = data.substring(index,index+1);
			graphics.drawString(str, 20*i, 30);
<span style="white-space:pre">			</span>buffer.append(str);
		}
HttpSessin session = request.getsession();

 
sesion.setAttribute("number",buffer);

这样就ok啦,简单的验证码功能书写完毕。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值