验证码校验小案例
作者:钟华
需求
-
使用graphics创建图片;
-
在图片上随机生成四位数的验证码,包含英文字母和数字;
-
从键盘获取用户输入的信息;
-
实现页面跳转:
- 用户输入信息 = 随机产生信息 -->输出true
- 用户输入信息 != 随机产生信息 -->输出false
-
以web程序为开发目标
开发步骤
-
创建工程:new project
-
导包:jstl-1.2.jar 和 standard-1.1.2.jar
-
在WebContent里面新建servlet文件并命名为ImageServlet.java
ImageServlet.java文件代码如下:
package com.jy.loginAndReg;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ImageServlet
*/
@WebServlet("/ImageServlet")
public class ImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ImageServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.创建BufferedImage对象
BufferedImage bImage = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
//2.生成graphics对象:
Graphics graphics = bImage.getGraphics();
Color color = new Color(255,211,240);
graphics.setColor(color);
graphics.fillRect(0, 0, 68, 22);
//3.通过random产生验证信息并使用graphics创建图片
char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
Random r = new Random();
int len = ch.length ,index;
String str = "";
for(int i=0;i<4;i++){
index = r.nextInt(len);
graphics.setColor(new Color(r.nextInt(225),r.nextInt(225),r.nextInt(225)));
graphics.drawString(ch[index]+"",(i*15)+5 ,16 );
str += ch[index];
}
//4.将验证码信息保存在session中
request.getSession().setAttribute("code", str);
//5.使用ImageIO输出图片
ImageIO.write(bImage,"JPG",response.getOutputStream());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
代码详解:
利用
request.getSession().setAttribute
方法将验证码信息保存在session域中。
- 在WebContent里面新建jsp文件并命名为code.jsp
code.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>验证码</title>
<script type="text/javascript">
function change(image){
image.src="<%=request.getContextPath() %>/ImageServlet?time="+new Date().getTime();
} //路径不变则无法实现onclick事件,所以加当前时间这个参数使其改变
</script>
</head>
<body>
<form method="get" action="codeTest.jsp">
<input name="yzCode" type="text" size=6 />
<img src="<%=request.getContextPath() %>/ImageServlet" title="看不清,换一张"
οnclick="change(this);" style="cursor:pointer">
//request.getContextPath()方法输出该工程的地址
<input type="submit" value="确定">
</form>
</body>
</html>
代码详解:
使用
new Date().getTime();
方法获取当前时间;
路径不变则无法实现onclick事件,所以需要添加参数使其改变
request.getContextPath()
方法输出该工程的地址
- 在WebContent里面新建jsp文件并命名为codeTest.jsp
codeTest.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>验证码校验</title>
</head>
<body>
<%
String code = (String)session.getAttribute("code");
String yzCode = request.getParameter("yzCode");
if(yzCode.toUpperCase().equals(code)){
out.println(true);
}else{
out.println(false);
}
%><br>
<%=code %><br>
<%=yzCode %>
</body>
</html>
代码详解
利用.toUpperCase()方法将用户输入字母格式转成大写。