本文主要是介绍JavaWeb项目中生成验证码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在项目中使用servlet随机生成验证码
1.在jsp页面中验证码图片加入img标签,src为生成验证码的servlet的url;
<img id="codeImg" src="/AuthCode/code" onclick="refreshCode()"/>
2.在servlet中生成随机验证码
public class AuthCode extends HttpServlet {private static final int WIDTH = 60;//设置验证码图片宽度private static final int HEIGHT = 20;//设置验证码图片高度private static final int LENGTH = 4;//设置验证码长度//设置验证码随机出现的字符private static final String str = "1234567890" +"abcdefghijklmnopqrstuvwxyz" +"ABCDEFGHIJKLMNOPQRSTUVWXYZ";char[] chars = str.toCharArray();//将字符放在数组中方便随机读取private static Random random = new Random();protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置输出的类型为图片response.setContentType("image/jpeg");//设置不进行缓存response.setHeader("pragma", "no-cache");response.setHeader("cache-control", "no-cache");response.setHeader("expires", "0");BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_3BYTE_BGR);//画笔Graphics graphics = image.getGraphics();//设置背景颜色并绘制矩形背景graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, WIDTH, HEIGHT);//用于记录生成的验证码String code = null;//生成验证码并绘制for (int i = 0; i < LENGTH; i++) {String c = "" + chars[random.nextInt(str.length())];graphics.setColor(getColor());graphics.drawString(c, 10 * i + 10, 18);code += c;}//生成干扰点for (int i = 0; i < 50; i++) {graphics.setColor(getColor());graphics.drawOval(random.nextInt(60), random.nextInt(20), 1, 1);}//将生成的验证码存入session中,以便进行校验HttpSession session = request.getSession();session.setAttribute("code", code);//绘制图片graphics.dispose();//将图片输出到response中ImageIO.write(image, "JPEG", response.getOutputStream());}//随机生成颜色private Color getColor() {return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));}
}
3.为img标签添加onclick事件,点击更新验证码
<script type="text/javascript">function refreshCode() {var codeImg = document.getElementById("codeImg");var d = new Date();codeImg.src = "/AuthCode/code?s="+d;}
</script>
文件:
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><servlet><servlet-name>AuthCode</servlet-name><servlet-class>AuthCode</servlet-class></servlet><servlet-mapping><servlet-name>AuthCode</servlet-name><url-pattern>/code</url-pattern></servlet-mapping>
</web-app>
- index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>验证码</title></head><body><img id="codeImg" src="/AuthCode/code" onclick="refreshCode()"></body>
<script type="text/javascript">function refreshCode() {var codeImg = document.getElementById("codeImg");var d = new Date();codeImg.src = "/AuthCode/code?s="+d;}
</script>
</html>
- AuthCode.java
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;@WebServlet(name = "AuthCode")
public class AuthCode extends HttpServlet {private static final int WIDTH = 60;//设置验证码图片宽度private static final int HEIGHT = 20;//设置验证码图片高度private static final int LENGTH = 4;//设置验证码长度//设置验证码随机出现的字符private static final String str = "1234567890" +"abcdefghijklmnopqrstuvwxyz" +"ABCDEFGHIJKLMNOPQRSTUVWXYZ";char[] chars = str.toCharArray();//将字符放在数组中方便随机读取private static Random random = new Random();protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置输出的类型为图片response.setContentType("image/jpeg");//设置不进行缓存response.setHeader("pragma", "no-cache");response.setHeader("cache-control", "no-cache");response.setHeader("expires", "0");BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_3BYTE_BGR);//画笔Graphics graphics = image.getGraphics();//设置背景颜色并绘制矩形背景graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, WIDTH, HEIGHT);//用于记录生成的验证码String code = null;//生成验证码并绘制for (int i = 0; i < LENGTH; i++) {String c = "" + chars[random.nextInt(str.length())];graphics.setColor(getColor());graphics.drawString(c, 10 * i + 10, 18);code += c;}//生成干扰点for (int i = 0; i < 50; i++) {graphics.setColor(getColor());graphics.drawOval(random.nextInt(60), random.nextInt(20), 1, 1);}//将生成的验证码存入session中,以便进行校验HttpSession session = request.getSession();session.setAttribute("code", code);//绘制图片graphics.dispose();//将图片输出到response中ImageIO.write(image, "JPEG", response.getOutputStream());}//随机生成颜色private Color getColor() {return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));}
}
项目下载
这篇关于JavaWeb项目中生成验证码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!