java验证码程序

2/22/2017来源:ASP.NET技巧人气:713

一个简单的4位纯数字验证码程序:

登录界面login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>登录验证码</title>
	<meta http-equiv="PRagma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keyWords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/CSS" href="styles.css">
	-->
  </head>
  
  <body>
    <form action="${pageContext.request.contextPath}/servlet/LoginServlet" method="post">
    	用户名:<input type="text" name="username"/><br/>
    	密 码:<input type="password" name="password"/><br>
    	验证码:<input type="text" name="captcha" size="4"/>
		<img  src="${pageContext.request.contextPath}/servlet/CaptchaServlet" width="120" height="25"/><br>
		<input type="submit" value="登录"/>
    </form>
  </body>
</html>

CaptchaServlet.java

package cn.itcast.web.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
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 {
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int width = 120;
		int height = 25;
		//对象,代表内存中的图片,BufferedImage
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//构建一副内存图像
		//画笔,花图像
		Graphics g = image.getGraphics();//图像上的画笔
		//1、设置颜色,画一个边线
		g.setColor(Color.GREEN);
		g.drawRect(0, 0, width, height);
		//2、设置颜色,填充色
				g.setColor(Color.YELLOW);
				g.fillRect(1, 1, width-2, height-2);
				//3、画干扰线
				g.setColor(Color.GRAY);
				Random r = new Random();
				for (int i = 0; i < 9; i++) {
					g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
				}
				//4、随机验证码
				g.setColor(Color.RED);
				g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,15));
				int x = 20;
				for (int i = 0; i < 4; i++) {
					String num = r.nextInt(10)+"";
					g.drawString(num, x, 20);
					x+=20;
				}
				System.out.println();
		//输出到页面上:response的响应输出液,字节液
		ImageIO.write(image, "jpg", response.getOutputStream());
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}

配置文件:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <servlet-name>CaptchaServlet</servlet-name>
    <servlet-class>cn.itcast.web.controller.CaptchaServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>CaptchaServlet</servlet-name>
    <url-pattern>/servlet/CaptchaServlet</url-pattern>
  </servlet-mapping>

</web-app>
如果用ValidateCode.jar包,则:

CaptchaServlet.java

package cn.itcast.web.controller;

import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.dsna.util.images.ValidateCode;
//输出验证码图像
public class CaptchaServlet extends HttpServlet {
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				//宽度、高度、验证码数、干扰线数
				ValidateCode vc = new ValidateCode(120, 25, 4, 9);
				String code = vc.getCode();
				System.out.println(code);
//				把验证码存放到Httpsession中
				request.getSession().setAttribute("code", code);
				//用字节流输出对应的图片
				ImageIO.write(vc.getBuffImg(), "jpg", response.getOutputStream());
			}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}