首页 > 代码库 > servlet实现一次性验证码

servlet实现一次性验证码

      大致的思路:先用java生成验证码,然后将生成的验证码保存到session中,在表单提交时,将保存到session中的验证码与表单提交的验证码进行比较,然后输出不同的结果。其实还可以实现一个目标,在用户禁用cookie也能验证成功,那就需要url重写了。这个例子只是给了一个简单的验证码的实现,当然可以根据实际的需求写出更加精彩的验证码,比如“汉字”验证码,算术验证码。本文只是给怎样实现验证码提供了一个基本的思路。

ImageCodeUtil.java

package cn.zq.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * 图形码工具类
 * @author zq
 */
public class ImageCodeUtil {
	
	/**
	 * 下面配置的这些常量,最好的办法就是通过配置文件来获取,下面直接写死了
	 */
	
	/**
	 * 图形码的宽
	 */
	private static final int IMAGE_CODE_WIDTH = 120;
	
	/**
	 * 图形码的高
	 */
	private static final int IMAGE_CODE_HEIGHT = 60;
	
	/**
	 * 图形码个数
	 */
	private static final int IMAGE_CODE_NUM = 4;
	
	/**
	 * 图形码最多干扰线数量
	 */
	private static final int IMAGE_CODE_MAX_LINE_NUM = 15;
	
	/**
	 * 图形码最小干扰线数量
	 */
	private static final int IMAGE_CODE_MIN_LINE_NUM = 10;
	
	/**
	 * 图片类型
	 */
	private static final String IMAGE_TYPE = "jpg";
	
	/**
	 * 图形码取数
	 */
	private static final String IMAGE_CODE_DATA = http://www.mamicode.com/"ABCDEFGHIJKLMNOPQRSJUVWXYZabcdefghijklmnopqrsjuvwxyz0123456789";>

GetImageCodeServlet.java

package cn.zq.servlet;

import java.io.IOException;

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

import cn.zq.util.ImageCodeUtil;

public class GetImageCodeServlet extends HttpServlet {

	private static final long serialVersionUID = 5225185955711424724L;

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

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置浏览器不要缓存
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		response.setHeader("expires", "0");
		response.setContentType("image/jpeg");
		HttpSession session = request.getSession();
		ServletOutputStream sos = response.getOutputStream();
		String code = ImageCodeUtil.generateSimpleImageCode(sos);
		session.setAttribute("imageCode", code);
	}

}

FormServlet.java

package cn.zq.servlet;

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 FormServlet extends HttpServlet {

	private static final long serialVersionUID = -5699696644228229140L;

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

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		
		//获取客户端提交的图像码
		String clientImageCode = request.getParameter("imageCode");
		
		HttpSession session = request.getSession();

		//获取服务器端的图像码
		String serverImageCode = (String) session.getAttribute("imageCode");
		
		
		PrintWriter out = response.getWriter();
		
		if(serverImageCode != null){
			
			if(serverImageCode.equalsIgnoreCase(clientImageCode)){
				out.print("已经通过验证!");
			}else{
				out.print("验证码输入有误");
			}
			
			session.removeAttribute("imageCode");
		}else{
			out.print("验证码失效!");
		}
	}

}
form.html
<span style="color:#000000;"><!DOCTYPE html>
<html>
  <head>
    <title>输入验证码</title>
  </head>
  
  <body>
    <form  action="formServlet" method="post">
     	<input type="text" name="imageCode"><img  src=http://www.mamicode.com/"getImageCodeServlet" border="1px"> 
>web.xml文件的有关配置:

<span style="color:#000000;"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
    <servlet-name>GetImageCodeServlet</servlet-name>
    <servlet-class>cn.zq.servlet.GetImageCodeServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>FormServlet</servlet-name>
    <servlet-class>cn.zq.servlet.FormServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>GetImageCodeServlet</servlet-name>
    <url-pattern>/getImageCodeServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>FormServlet</servlet-name>
    <url-pattern>/formServlet</url-pattern>
  </servlet-mapping>
</web-app></span>

与验证码类似的经典问题有,怎样防止表单重复提交,这类问题很有意思,防止表单提交的要点就是在表单用隐藏域保存从服务器端获取的值,当表单提交时,拿这个值与服务器端的值进行比较。大致的思路与验证码很类型,都是在服务器端存放值,在从客户浏览器表单提交时获取客户端的值进行比较。源代码可以再这里下载



servlet实现一次性验证码