java实现图片验证码

时间:2023-02-05 08:38:58

这段时间一直在忙着搞课设,没顾得上写,今天终于找到了一点时间,赶快开写啦~

ImageServlet.java(image.do):用于生成验证码图片

package com.ymw.web.servlet;

import java.awt.Color;
import java.awt.Font;
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.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int width = 80;// 验证码图片宽度
int height = 26;// 验证码图片高度
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
Graphics g = image.getGraphics();
Random random = new Random();// 创建一个随机类
g.setColor(getRandColor(200, 250));// 背景颜色要偏淡
g.fillRect(0, 0, width, height);// 画背景
g.setColor(getRandColor(0, 255));// 边框颜色
g.drawRect(0, 0, width - 1, height - 1);// 画边框
g.setColor(getRandColor(160, 200));// 随机产生5条干扰线,使图象中的认证码不易被其它程序探测到
for (int i = 0; i < 8; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
g.drawLine(x, y, x1, y1);
}
g.setColor(getRandColor(160, 200));// 随机产生100点,使图象中的认证码不易被其它程序探测到
for (int i = 0; i < 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawLine(x, y, x, y);
}
Font font = new Font("Times New Roman", Font.ITALIC, 20); // 创建字体,字体的大小应该根据图片的高度来定。
g.setFont(font);// 设置字体
int length = 4; // 设置默认生成4个验证码
String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // 设置备选验证码:包括"a-z"和数字"0-9"
String sRand = "";

// 用随机产生的颜色将验证码绘制到图像中。
// 生成随机颜色(因为是做前景,所以偏深)
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.setColor(new Color(20 + random.nextInt(110),
20 + random.nextInt(110), 20 + random.nextInt(110)));
for (int i = 0; i < length; i++) {
String ch = String.valueOf(s.charAt(random.nextInt(s.length())));
sRand += ch;
g.drawString(ch, 11 * i + 12, (random.nextInt(5) - 2) * i + 20);
}
// 将生成的字符串存储在session中(这点很重要!之后验证时要用到!)
HttpSession session = request.getSession();
session.setAttribute("checkCode", sRand);
g.dispose();// 图像生效
// 禁止图像缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
// 创建二进制的输出流
ServletOutputStream sos = response.getOutputStream();
// 将图像输出到Servlet输出流中。
ImageIO.write(image, "jpeg", sos);
sos.flush();
sos.close();
}

public Color getRandColor(int lower, int upper) {
Random random = new Random();
if (upper > 255)
upper = 255;
if (upper < 1)
upper = 1;
if (lower < 1)
lower = 1;
if (lower > 255)
lower = 255;
int r = lower + random.nextInt(upper - lower);
int g = lower + random.nextInt(upper - lower);
int b = lower + random.nextInt(upper - lower);
return new Color(r, g, b);
}
}

login.jsp:使用到验证码的页面(该页面要提交给Login.do—LoginServlet.java)

<script>
function changeImage(){
var obj = document.getElementById("image");
obj.src = "image.do?r="+Math.random();
}
</script>

。。。

<tr>
<td class="field">验证码:</td>
<td><input type="text" name="veryCode" /><img id="image" src="image.do" /><a onclick="changeImage()" style="cursor:pointer">看不清楚?点击这里更换</a><br />${imageCodeResult }</td>
</tr>

style=”cursor:pointer”鼠标移上是手型;

LoginServlet.java:用于处理login.jsp提交上来的请求

public class LoginServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("utf-8");//解决乱码问题
String veryCode = request.getParameter("veryCode");//得到用户输入的验证码

//先将从session中获取的验证码与用户输入的验证码进行比较,如果相同再验证用户名和密码,否则将错误信息带回登陆界面
if(veryCode.equals(session.getAttribute("checkCode"))){

//进行其他信息的验证

}else {

String imageCodeResult="验证码输入错误,请重新输入!";
//千万不要忘记把表示验证情况的信息放到request中去,否则前台是不能看到的
request.setAttribute("imageCodeResult", imageCodeResult);
request.getRequestDispatcher("login.jsp")
.forward(request, response);

}
}
}

java实现图片验证码

java实现图片验证码