近日 ,遇到和验证码问题,在网上找了很多资料加自己的总结,得到可实现代码
login.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>oa自动化办公系统</title><link rel="stylesheet" href="/soa/css/login.css" /><script type="text/javascript" src="/soa/js/jquery-3.0.0.js" ></script><script type="text/javascript"> $(function() { $("#verifyimg").click(function() { $.get("/soa/identCodeServlet.do", function(data) { //此处是刷新的关键 $("#verifyimg").attr("src", "/soa/identCodeServlet.do"); }); }); });</script></head><body> <div id="login"> <div> <form action="" method="post"> <table> <tr><td colspan="3"><h1>oa自动办公系统</h1><span id="s_1">登陆</span></td></tr> <tr><td class="f1">用户名:</td><td colspan="2"><input class="in1" type="text" name="lname" /></td></tr> <tr><td class="f1">密 码:</td><td colspan="2"><input class="in1" type="password" name="lpwd" /></td></tr> <tr><td class="f1">验证码:</td><td><input class="in1" id="v1" maxlength="4" type="text" name="verify " /></td><td><img id="verifyimg" src="/soa/identCodeServlet.do"></td></tr> <tr><td colspan="3"><input id="submit" type="submit" value="登陆" /></td></tr> </table> </form> </div> </div></body></html>IdentCodeServlet.java 代码
package com.eduask.oa.servlet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.eduask.oa.util.CreateRandomCode;/** * 将生成的验证码作为图片输出 * @author Maibenben * */public class IdentCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 禁止浏览器缓存随机图片 response.setDateHeader("Expires", -1); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); //2.通知服务器以图片的方式发送数据 response.setHeader("Content-Type", "image/jpeg"); //3.在内存创建一张图片 BufferedImage image = new BufferedImage(150, 50, BufferedImage.TYPE_INT_BGR); //4.向图片上写数据 Graphics g = image.getGraphics(); //5.设置背景色 g.setColor(Color.gray); g.fillRect(0, 0, 150, 50); //6. 设置写入数据的颜色和字体 g.setColor(Color.red); g.setFont(new Font(null, Font.BOLD, 50)); // 7.想图片上写数据 String randomNum = new CreateRandomCode().createRandomCode(); request.getSession().setAttribute("checkRandomCode", randomNum); g.drawString(randomNum, 20, 40); System.out.println(randomNum); // 8.把写好的图片输出到浏览器 ImageIO.write(image, "jpg", response.getOutputStream()); }}生成4位随机验证码代码 CreateRandomCode.java
package com.eduask.oa.util;/** * 随机产生4位 数字或字母混合 * @author Maibenben * */public class CreateRandomCode { private char[] code = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; /** * 生成4位验证码 * @return */ public String createRandomCode() { char[] codeRandom = new char[4]; //循环取得其中随机字符 for(int i=0; i< codeRandom.length; i++) { int random = (int)(Math.random()*code.length); codeRandom[i] = code[random]; } //将字符数组转为String StringBuffer sb = new StringBuffer(); for(int i=0; i<codeRandom.length; i++) { sb.append(codeRandom[i]); } return sb.toString(); }}这只是简单的验证码图片刷新,希望可以帮到正在寻找答案的你