Servlet----验证码问题

在我们的日常上网中,经常会遇到验证码的输入,有时候我们会被这些头疼的验证码弄的很烦躁,特别是在12306上买票的时候,那些千奇百怪的验证码。。。。今天我把关于一些验证码的编写代码分享给大家。。


1、简单版

@Test
	public void demo1() throws IOException {
		BufferedImage img = new BufferedImage(60, 30,BufferedImage.TYPE_INT_RGB );
		Graphics g = img.getGraphics();
		g.drawString("Hello", 0, 30);版本2是扩展这一句
		//把图形刷到img对象中
		g.dispose();//相当于IO中的close()方法带动flush()
		ImageIO.write(img, "JPEG", new FileOutputStream("d:/a/hello.jpg"));
	}

 

2、过渡版

@Test
	public void demo2() throws IOException {
		int w = 60;
		int h = 30;
		BufferedImage img = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
		Graphics g = img.getGraphics();
		
		//背景
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, w, h);
		//字体
		g.setFont(new Font("aa", Font.BOLD, 18));
		//输出验证码: 4个0~9之间的随机整数
		Random r = new Random();
		for(int i=0;i<4;i++){
			int a = r.nextInt(10);
			int y = 10+r.nextInt(20);//上下位置:10~30
			Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
			g.setColor(c);
			g.drawString(""+a, i*16, y);
		}
		//画干扰线
		for(int i=0;i<20;i++){
			Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
			g.setColor(c);
			g.drawLine(r.nextInt(w), r.nextInt(h), r.nextInt(w), r.nextInt(h));
		}
		
		//把图形刷到img对象中
		g.dispose();//相当于IO中的close()方法带动flush()
		ImageIO.write(img, "JPEG", new FileOutputStream("d:/a/hello.jpg"));
	}


3、实用版

@Test
	public void demo3() throws IOException {
		int w = 60;
		int h = 30;
		BufferedImage img = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
		Graphics g = img.getGraphics();
		Graphics2D g2d = (Graphics2D)g;
		
		//背景
		g2d.setColor(Color.WHITE);
		g2d.fillRect(0, 0, w, h);
		//字体
		g2d.setFont(new Font("aa", Font.BOLD, 18));
		//输出验证码: 4个0~9之间的随机整数
		Random r = new Random();
		for(int i=0;i<4;i++){
			int a = r.nextInt(10);
			int y = 10+r.nextInt(20);//上下位置:10~30
			Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
			g2d.setColor(c);
			//旋转与放缩
			AffineTransform tx = new AffineTransform();
			tx.rotate(r.nextDouble(), i*16, y);
			tx.scale(r.nextDouble(),r.nextDouble());
			g2d.setTransform(tx);
			g.drawString(""+a, i*16, y);
		}
		//画干扰线
		for(int i=0;i<10;i++){
			Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
			g2d.setColor(c);
			g2d.drawLine(r.nextInt(w), r.nextInt(h), r.nextInt(w), r.nextInt(h));
		}
		
		//把图形刷到img对象中
		g2d.dispose();//相当于IO中的close()方法带动flush()
		ImageIO.write(img, "JPEG", new FileOutputStream("d:/a/hello.jpg"));
	}


当然其中的语句算法是可以编写的更为复杂使得破译难度提高


上面编写代码只是供大家参考

下面是在JAVA项目的实现  在Tomca服务器的帮助下完成。

首先是在index.jsp中编写


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>第一个Web项目的主页</title>
    <script type="text/javascript">
    	function changeImg(){
    		var _img = document.getElementById("_img");
    		var d = new Date();
    		var t = d.getTime();
    		_img.src = "aa?"+t;
    	}
    </script>
    
  </head>
  
  <body>
	
	<form action="hh" method="get">
		姓名:<input type="text" name="name" /><br/>
		密码:<input type="password" name="pwd" /><br/>
		确认密码:<input type="password" name="pwd2" /><br/>
		验证码:<input type="text" name="acode" /><img id="_img" src="aa" /><a href="javascript:changeImg();">看不清</a><br/>
		<input type="submit" value="注册"/>
	</form>
	
	
  </body>
</html>

然后在项目中编写
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
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 ImageServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = req.getParameter("name");
		System.out.println("name: "+name);
		//System.out.println("aaaaa");
		resp.setContentType("image/jpeg");//1 ※※设置响应内容的类型为jpg图片
		
		int w = 60;
		int h = 30;
		BufferedImage img = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
		Graphics g = img.getGraphics();
		
		//背景
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, w, h);
		//字体
		g.setFont(new Font("aa", Font.BOLD, 18));
		//输出验证码: 4个0~9之间的随机整数
		Random r = new Random();
		for(int i=0;i<4;i++){
			int a = r.nextInt(10);
			int y = 10+r.nextInt(20);//上下位置:10~30
			Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
			g.setColor(c);
			g.drawString(""+a, i*16, y);
		}
		//画干扰线
		for(int i=0;i<20;i++){
			Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
			g.setColor(c);
			g.drawLine(r.nextInt(w), r.nextInt(h), r.nextInt(w), r.nextInt(h));
		}
		
		//把图形刷到img对象中
		g.dispose();//相当于IO中的close()方法带动flush()
		
		ImageIO.write(img, "JPEG", resp.getOutputStream() );//2通过resp获取outputStream,写到该输出流即写到客户端   resp----发向客户端的socket的封装
		
	}
}


以上是一个完整的验证码项目的实现


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值