首页 > 代码库 > 验证码技术示例

验证码技术示例

        继续总结与编程相关的技术,今天总结的是大学期间关于验证码技术的一个小示例:

        代码1——生成验证码的jsp文件,文件名:captcha.jsp:

<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*,java.io.*" %>
<html>
	<body>
		<%!
			Color getRandColor(int fc,int bc){
				Random random = new Random();
				if(fc>255) fc=255;
				if(bc>255) bc=255;
				int r=fc+random.nextInt(bc-fc);
				int g=fc+random.nextInt(bc-fc);
				int b=fc+random.nextInt(bc-fc);
				return new Color(r,g,b);
			}
		%>
		<%
			response.setHeader("Pragma","No-cache");
			response.setHeader("Cache-Control","no-cache");
			response.setDateHeader("Expires", 0);
			int width=60;
			int height=20;
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics graphics = image.getGraphics();
			graphics.setColor(getRandColor(200,250));
			graphics.fillRect(0, 0, width, height);
			graphics.setFont(new Font("Times New Roman",Font.PLAIN,18));
			graphics.setColor(getRandColor(160,200));
			graphics.drawRect(0,0,width-1,height-1);
			Random random = new Random();
			for (int i=0;i<155;i++){
				int x = random.nextInt(width);
				int y = random.nextInt(height);
				int xl = random.nextInt(12);
				int yl = random.nextInt(12);
				graphics.drawLine(x,y,x+xl,y+yl);
			}
			String sRand="";
			for (int i=0;i<4;i++){
				String rand=String.valueOf(random.nextInt(10));
				sRand+=rand;
				graphics.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));	
				graphics.drawString(rand,13*i+6,16);
			}
			session.setAttribute("rand",sRand);
			graphics.dispose();
			ImageIO.write(image,"JPEG",response.getOutputStream());
			out.clear();
			out = pageContext.pushBody();
		%>
	</body>
</html>
        代码2——使用验证码的jsp文件,文件名:index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
	<head>
		<title>验证码的使用</title>
		<script type="text/javascript">		
	  		function reloadImage(){
	  			document.getElementById("safecode").src=http://www.mamicode.com/"captcha.jsp?seed"+Math.random();>        代码3——异步校验验证码的java文件,文件名:AjaxCheckCaptchaServlet:
package com.ghj.packageofservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AjaxCheckCaptchaServlet extends HttpServlet {

	private static final long serialVersionUID = 6312962948471033343L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		doPost(request,response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
		String yanzhengma=request.getParameter("yanzhengma_value");//获取用户在“验证码”文本框中输入的验证码,引号中的值为引号传参中的key。
		System.out.println("异步验证————用户输入的验证码是:"+yanzhengma);
		String rand = ((String)session.getAttribute("rand")).trim();//得到原验证码,"rand"来自captcha.jsp
	    System.out.println("异步验证————原验证码是:"+rand);
		if (rand.equals(yanzhengma)) {
			out.print("1");//“1”表示用户输入的验证码和原验证码一致。
		} else {
			out.print("2");//“2”表示用户输入的验证码和原验证码不一致。
		}
		out.flush();
		out.close();
	}
}
        代码4——配置Servlet的xml文件,文件名:web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">

  <servlet>
    <servlet-name>AjaxCheckCaptchaServlet</servlet-name>
    <servlet-class>com.ghj.packageofservlet.AjaxCheckCaptchaServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AjaxCheckCaptchaServlet</servlet-name>
    <url-pattern>/AjaxCheckCaptchaServlet</url-pattern>
  </servlet-mapping>
  	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

        演示说明:将验证码输入到文本框后,请让该文本框失去焦点,程序采用异步的方式校验所输入的验证码和原来生成的验证码是否一致。

        【0分下载资源