Servlet的方式实现验证码
生成验证码的servlet:CodeServlet//这个生成验证码的servlet已经将随机数(也就是这里的验证码)放入session中,以便于校验时获取req.getSession().setAttribute(“scode”, randomString);System.out.println(randomString);package com.web.servlet;import...
·
生成验证码的servlet:CodeServlet(区分大小写)
//这个生成验证码的servlet已经将随机数(也就是这里的验证码)放入session中,以便于校验时获取
req.getSession().setAttribute(“scode”, randomString);
System.out.println(randomString);
package com.web.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
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 CodeServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int WIDTH = 120;//生成图片的宽度
public static final int HEIGHT = 30;//生成图片的高度
public static final int WORDS_NUMBER = 4;//验证码中字符的个数
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String createTypeFlag = req.getParameter("createTypeFlag");//接收客户端传递的createTypeFlag标识
//在内存中创建一张图片
BufferedImage bi = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_3BYTE_BGR);
//得到图片
Graphics g = bi.getGraphics();
//设置图片的背景色
setBackGround(g);
//设置图片的边框
setBorder(g);
//在图片上画干扰线
drawRandomLine(g);
//在图片上放上随机字符
String randomString = this.drawRandomNum((Graphics2D)g, createTypeFlag);
//将随机数存在session中
req.getSession().setAttribute("scode", randomString);
System.out.println(randomString);
//设置响应头通知浏览器以图片的形式打开
resp.setContentType("image/jpeg");
//设置响应头控制浏览器不要缓存
resp.setDateHeader("expries", -1);
resp.setHeader("Cache-Control", "no-cache");
resp.setHeader("Pragma", "no-cache");
//将图片传给浏览器
ImageIO.write(bi, "jpg", resp.getOutputStream());
}
//设置图片背景色
//@param g
private void setBackGround(Graphics g) {
//设置颜色
g.setColor(Color.WHITE);
//填充区域
g.fillRect(0, 0, WIDTH, HEIGHT);
}
/*
* 设置图片的边框
* @param g
* */
private void setBorder(Graphics g) {
//设置边框颜色
g.setColor(Color.BLUE);
//边框区域
g.drawRect(1, 1, WIDTH - 2, HEIGHT -2);
}
/*
* 在图片上画随机线条
* @param g
* */
private void drawRandomLine(Graphics g) {
//设置颜色
g.setColor(Color.GREEN);
//设置线条个数并画线
for ( int i = 0 ; i < 3 ; i++ ) {
int x1 = new Random().nextInt(WIDTH);
int y1 = new Random().nextInt(HEIGHT);
int x2 = new Random().nextInt(WIDTH);
int y2 = new Random().nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
}
/*
* 在图片上画随机字符
* @param g
* @param createTypeFlag
* @return String
* */
private String drawRandomNum(Graphics g,String createTypeFlag) {
//设置颜色
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD,20));
//数字字母的组合
String baseNumLetter = "0123456789ABCDEFGHJKLMNOPQRSTUVWXYZ";
String baseNum = "0123456789";
String baseLetter = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
if ( createTypeFlag != null && createTypeFlag.length() > 0 ) {
if( createTypeFlag.equals("nl") ) {
//截取数字和字母的组合
return createRandomChar((Graphics2D) g,baseNumLetter);
} else if ( createTypeFlag.equals("n") ) {
//截取数字的组合
return createRandomChar((Graphics2D) g,baseNum);
} else if ( createTypeFlag.equals("l") ) {
//截取字母的组合
return createRandomChar((Graphics2D) g,baseLetter);
}
} else {
//截取数字和字母的组合
return createRandomChar((Graphics2D) g,baseNumLetter);
}
return "";
}
/*
* 创建随机字符
* @param g
* @param baseChar
* @return String
* */
private String createRandomChar(Graphics2D g , String baseChar) {
StringBuffer b = new StringBuffer();
int x = 5;
String ch = "";
for ( int i = 0 ; i < WORDS_NUMBER ; i++ ) {
//设置字体的旋转角度
int degree = new Random().nextInt() % 30;
ch = baseChar.charAt(new Random().nextInt(baseChar.length())) + "";
b.append(ch);
//正向角度
g.rotate(degree * Math.PI / 180 , x,20);
g.drawString(ch, x, 20);
//反向角度
g.rotate(-degree * Math.PI / 180 , x,20);
x+=30;
}
return b.toString();
}
}
验证码展示:运行CodeServlet
做一个验证码提交页面:yanzhengma.jsp(这里面调用了CodeServlet,效果如下)
<%@ 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>
<script>
function changeImg()
{
document.getElementById("codeImg").src="CodeServlet?i="+Math.random();
}
</script>
</head>
<body>
<form action="CheckCodeServlet" method="post">
验证码:<input type="text" name="ccode">
<img alt="验证码看不清,换一张" src="CodeServlet" id="codeImg" onclick="changeImg()">
<a href="#" onclick="changeImg()">看不清,换一张</a>
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
对验证码进行验证的servlet:CheckCodeServlet
package com.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CheckCodeServlet
*/
public class CheckCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//客户端那边输入的验证码
String cCode = request.getParameter("ccode");
//生成验证码页面放入域中的验证码
String sCode = (String) request.getSession().getAttribute("scode");
//System.out.println(cCode);
//System.out.println(sCode);
if(cCode.equals(sCode))
{
response.getWriter().println("校验成功!");
}else{
response.getWriter().println("校验失败!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
回到yanzhengma.jsp页面,输入验证码
校验成功
校验失败
更多推荐
已为社区贡献2条内容
所有评论(0)