首页 > 代码库 > Session技术案例-用户注册验证码

Session技术案例-用户注册验证码

项目名称:/sessionVerificationCode

项目目录:

项目源码:

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=request.getAttribute("msg") %>
<form action="/sessionVerificationCode/RegisterServlet" method="post">
<input type="text" name="checkCode"></input>
验证码:
<img src=http://www.mamicode.com/"/sessionVerificationCode/VCodeGenerateServlet" onclick="this.src=‘/sessionVerificationCode/VCodeGenerateServlet?‘ + new Date()">
>

 

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	注册成功
</body>
</html>


 

RegisterServlet.java

package com.ghsy.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("dopost RegisterServlet");
		//1 获得数据
		// * 用户提交
		String checkCode = request.getParameter("checkCode");
		// * 服务器缓存 
		String sessionBufferData = (String) request.getSession().getAttribute("sessionBufferData");
		System.out.println("sessionBufferData:"+sessionBufferData);
		System.out.println("checkCode"+checkCode);
		//2比较
		if(sessionBufferData != null){ //服务器有缓存
			// * 完成一次性,将session缓存移除
			request.getSession().removeAttribute("sessionBufferData");
			if(sessionBufferData.equalsIgnoreCase(checkCode)){
				// 用户输入正确
				response.sendRedirect("success.jsp");
			} else {
				//不正确
				request.setAttribute("msg", "验证码不正确,请重新输入");
				request.getRequestDispatcher("/index.jsp").forward(request, response);
			}
		} else {
			request.setAttribute("msg", "请不要重复提交");
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
	}
}


 

VCodeGenerateServlet.java

package com.ghsy.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
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 VCodeGenerateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 //img标签生成图片的方式,是get请求
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doget VCodeGenerateServlet");
		//初始化图片的基本参数
		int width=80;
		int height=40;
		String vcodeSource="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		Random random=new Random();
		//创建一个rgb类型的图片
		BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
		//创建画板-图层
		Graphics2D g=image.createGraphics();
		//设置相关属性
		g.setColor(Color.black);
		g.fillRect(0, 0, width, height);//用当前的颜色填充画一个矩形
		g.setColor(Color.white);
		g.fillRect(1, 1, width-2, height-2);
		g.setColor(Color.RED);
		g.setFont(new Font("宋体", Font.BOLD, 20	));
		//在图片中写入字符
		//#定义StringBuilder存储四个随机字符
		StringBuilder sb=new StringBuilder();
		//#随机的在vcodeSource字符串中选出四个字符,作为验证码
		for (int i = 0; i < 4; i++) {
			int index=random.nextInt(vcodeSource.length());
			String str=vcodeSource.substring(index, index+1);
			sb.append(str);	
			int rotateNum = random.nextInt(30);
			//设置角度
			g.rotate((rotateNum - 15) * Math.PI / 180 );
			g.drawString(str, (width / 6) * (i+1), 20);
			g.rotate(- ((rotateNum - 15) * Math.PI / 180 ));
		}
		//**#3 把生成数据放置session
		String bufferData = http://www.mamicode.com/sb.toString();>