1.前端jsp界面只是一个img标签用来显示验证码,login.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>验证码</title>
</head>
<body>
<!-- ?+Math.random()是因为浏览器有缓存,如果不修改访问请求,不会去服务器查询数据 -->
<!-- 图片的src为servlet请求-->
<img alt="" src="checkcode" border='1' οnclick="this.src='checkcode?'+Math.random();" id = "code">
<a href="JavaScript:void(0)" onclick="document.getElementById('code').src='checkcode?'+Math.random();">看不清,换一张</a>
</body>
</html>
2.相应的servlet,在项目controler包中新建Servlet,名为ChekCodeServlet,代码如下:
package controler;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CheckCodeServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* 一、绘图
*/
//1画布
BufferedImage image = new BufferedImage(90, 30, BufferedImage.TYPE_INT_RGB);
//2获得画笔
Graphics g = image.getGraphics();
//3给画笔设置颜色
g.setColor(new Color(255,255,255));
//4给画布设置背景颜色
g.fillRect(0, 0, 90, 30);
g.setFont(new Font(null, Font.ITALIC, 24));
//5重试给画笔设置颜色
Random r = new Random();
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
//6生成一个随机数,介于[0,99999)之间
// String number = r.nextInt(99999)+"";
//生成一个长度为5的验证码,在A~Z,2~9之间,去掉I和O
String number = getNumber();
//画图
g.drawString(number, 2, 25);
//画干扰线
for (int i = 0; i < 8; i++) {
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.drawLine(r.nextInt(90), r.nextInt(30), r.nextInt(90), r.nextInt(30));
}
/*
* 二 、压缩图片,输出
* */
response.setContentType("image/jpg");
//获得字节输出流。之前用的out 是字符流不可以用了(管道)
OutputStream ops = response.getOutputStream();
//压缩图片并输出,算法有现成的方法可用
//imagoio中有一个类
javax.imageio.ImageIO.write(image, "jpeg", ops);
//关闭管道
ops.close();
}
private String getNumber() {
String number = "";
String chars = "ABCDEFGHGKLMNPQRSTUVWXYZ23456789";
Random r = new Random();
for (int i = 0; i < 5; i++) {
number += chars.charAt(r.nextInt(chars.length()));
}
return number;
}
}
3.web.xml中的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>CheckCodeServlet</servlet-name>
<servlet-class>controler.CheckCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckCodeServlet</servlet-name>
<url-pattern>/checkcode</url-pattern>
</servlet-mapping>
</web-app>
作者:jiaqio
来源:CSDN
原文:https://blog.csdn.net/qq_21235455/article/details/86589963
版权声明:本文为博主原创文章,转载请附上博文链接!