步骤:
1、页面显示
index.jsp
2、生成图片用到的类
ImageServlet
ImageServlet
3、校验验证码是否正确
使用loginServlet 进行验证
4、刷新验证码
javaScript
5、程序代码结构
(1)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>验证码页面</title>
</head>
<!-- 1、使用java实现验证码制作-->
<!-- 2、使用JavaScript实现验证码刷新-->
<script type="text/javascript">
function reloadCode(){
var img =document.getElementById("img");
img.src="ImageServlet";//验证码的生成地址
}
</script>
<body>
<form action="<%=request.getContextPath() %>/LoginServlet" method="post">
验证码:<input type="text" name="checkcode"/>
<img id="img" alt="验证码" src="<%=request.getContextPath()%>/ImageServlet"/>
<a href="" οnclick="reloadCode()">看不清楚</a><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
(2)ImageServlet类
package servlet;
/*1、使用java实现验证码制作
* 2、使用JavaScript实现验证码刷新*/
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;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.*;
/**
* Servlet implementation class 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 {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int height = 20;
int width = 60;
//创建BufferedImage对象,参数:高度、宽度、图片类型
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取Graphic类对象
Graphics g = bi.getGraphics();
Color c =new Color(200,200,200);//背景颜色
//设定背景
g.setColor(c);
g.fillRect(0, 0, width, height);
//定义随机字符数组
char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
//获取4随机数
Random r = new Random();
int len = ch.length;
//字符数组下标
int index;
//装载随机数的容器
StringBuffer sb = new StringBuffer();
for(int i = 0; i < 4 ;i++){
index = r.nextInt(len);
g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));//设置字符颜色
g.setFont(new Font("",Font.PLAIN,20));//加粗随机数
g.drawString(ch[index]+"", (i*15), 18);//加载字符串到图片上(随机数、横坐标、纵坐标)
sb.append(ch[index]);//存字符
}
//随机产生100个干扰点,使图像不被其他程序探测到
for(int i = 0 ;i < 100;i++){
int x = r.nextInt(width);
int y = r.nextInt(height);
g.drawOval(x, y, 1, 1);
}
//将验证码存入session
request.getSession().setAttribute("piccode", sb.toString());
//输出图片
ImageIO.write(bi, "JPEG", response.getOutputStream());
}
}
(3)LoginServlet类
package 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;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//一定要在输出之前写出改代码,否则无效
response.setContentType("text/html;charset=utf-8");
String checkcode = request.getParameter("checkcode");
String piccode = (String) request.getSession().getAttribute("piccode");
PrintWriter out = response.getWriter();//获取输出流
if(checkcode.toUpperCase().equals(piccode)){
out.println("验证码输入正确!");
}else{
out.println("验证码输入错误!");
}
out.flush();
out.close();
}
}