HTML代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/day16/ResualtServlet" method="post">
验证码:<input type="text" name="code"><br>
<img alt="图片无法显示" id="imgId" src="/day16/VerifyLoginServlet" onclick="changeImg()">
<a href="javascript:void(0)" onclick="changeImg()">看不清,换一张</a><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<script>
function changeImg(){
document.getElementById("imgId").src="/day16/VerifyLoginServlet?"+new Date();
}
</script>
Java代码
生成验证码
public class VerifyLoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//生成验证码字符串和图片
BufferedImage img = VerifyCodeUtils.paintImage(80, 30);
String code = VerifyCodeUtils.getCode();
//把验证码存放到session对象中
HttpSession session = request.getSession();
session.removeAttribute("code");
session.setAttribute("code", code.toLowerCase());
System.out.println(session.getAttribute("code"));
//生成验证码
ImageIO.write(img, "jpeg", response.getOutputStream());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
验证输入验证码是否正确
public class ResualtServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String parameter = request.getParameter("code").toLowerCase();
String code = (String) request.getSession().getAttribute("code");
if (parameter.equals(code)) {
response.getWriter().println("验证成功");
} else {
response.getWriter().println("验证失败");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
验证码生成工具
public class VerifyCodeUtils{
private static StringBuffer codesave = null;
public static BufferedImage paintImage(int width, int height) {
//核心对象,第一个参数是图片的宽度,第二个参数是高度,最后一个参数是颜色的模式
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获得画笔
Graphics graphics = img.getGraphics();
//给画笔设置为黑色
graphics.setColor(Color.BLACK);
//绘制矩形
graphics.fillRect(0, 0, width, height);
//给画笔设置为白色
graphics.setColor(Color.WHITE);
graphics.fillRect(1, 1, width-2, height-2);
//定义字符数据
String code = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
Random rd = new Random();
codesave = new StringBuffer();
//设置画笔的字体样式为楷体、加粗,行高
graphics.setFont(new Font("楷体", Font.BOLD, height-4));
for (int i = 0; i < 4; i++) {
//设置颜色
graphics.setColor(new Color(rd.nextInt(256), rd.nextInt(256), rd.nextInt(256)));
//取出随机索引
int index = rd.nextInt(code.length());
codesave.append(code.substring(index, index+1));
//画字符串
graphics.drawString(code.substring(index, index+1), width/6*(i+1), height-4);
}
return img;
}
public static String getCode() {
if (codesave==null) {
return "";
}
return codesave.toString();
}
}