生成验证码的servlet

public class ImgServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final int WIDTH = 120;
	public static final int HEIGHT = 25;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
		
		Graphics g = image.getGraphics();
		
		//1、设置背景色
		setBackGroud(g);
		
		//2.设置边框	
		setBorder(g);
		
		//3.画干扰线
		drawRandomLine(g);
		
		//4.写随机数
		String random = drawRandomNum((Graphics2D)g);
		request.getSession().setAttribute("checkcode", random);
		
		//5.图形写给浏览器 
		response.setContentType("image/jpeg");
		//发图控制浏览器不要缓存
		response.setDateHeader("expries", -1);
		response.setHeader("cache-control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ImageIO.write(image, "jpg", response.getOutputStream());
	}
	
	private void setBackGroud(Graphics g) {
		g.setColor(Color.WHITE);
		//填充矩形
		g.fillRect(0, 0, WIDTH, HEIGHT);
	}

	private void setBorder(Graphics g) {
		g.setColor(Color.BLUE);
		
		g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
	}
	
	private void drawRandomLine(Graphics g) {
		g.setColor(Color.GREEN);
		
		for(int i=0; i<5; i++){
			int x1 = new Random().nextInt(WIDTH);
			int y1 = new Random().nextInt(HEIGHT);
			int x2 = new Random().nextInt(WIDTH);
			int y2 = new Random().nextInt(HEIGHT);
			g.drawLine(x1, y1, x2, y2);
		}
	}
	
	//产生随机数并返回
	private String drawRandomNum(Graphics2D g) {
		g.setColor(Color.RED);
		g.setFont(new Font("宋体",Font.BOLD,20));
		
		//中文字所在区间  [\u4e00-\u9fa5]
		String base = "ABCDEFGHIJK";
		int x = 10;
		
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<4; i++){
			
			//设置旋转角度
			int degree = new Random().nextInt()%30;//生成正负30内数
			String ch = base.charAt(new Random().nextInt(base.length()))+"";
			sb.append(ch);
			
			g.rotate(degree*Math.PI/180, x, 20);
			g.drawString(ch, x, 20);
			g.rotate(-degree*Math.PI/180, x, 20);//旋转回去
			x+=30;
		}
		return sb.toString();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}



提交表单

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	//实现图片转换的函数
	/* function changeImage(img){
		img.src = img.src+"?"+new Date().getTime();
	} */
</script>
</head>
<body>

	<form action="/day07/ImgCheck">
	用户名:<input type="text" name="username"><br>
	密    码:<input type="password" name="password"><br>
	验证码:<input type="text" name="checkcode">
	<img src="/day07/ImgServlet" οnclick="this.src=this.src+'?'+new Date().getTime()" alt="换一张" style="cursor:pointer"><br>
	<input type="submit" value="提交">
	</form>
</body>
</html>



验证的servlet

public class ImgCheck extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String c_checkcode = request.getParameter("checkcode");
		String s_checkcode = (String) request.getSession().getAttribute("checkcode");
		
		if(c_checkcode!=null && s_checkcode!=null && c_checkcode.equals(s_checkcode)){
			System.out.println("登陆成功");
		}else{
			System.out.println("验证失败");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}