您好,登錄后才能下訂單哦!
近日 ,遇到和驗證碼問題,在網上找了很多資料加自己的總結,得到可實現代碼
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"><h2>oa自動辦公系統</h2><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.p_w_picpath.BufferedImage; import java.io.IOException; import javax.p_w_picpathio.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", "p_w_picpath/jpeg"); //3.在內存創建一張圖片 BufferedImage p_w_picpath = new BufferedImage(150, 50, BufferedImage.TYPE_INT_BGR); //4.向圖片上寫數據 Graphics g = p_w_picpath.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(p_w_picpath, "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(); } }這只是簡單的驗證碼圖片刷新,希望可以幫到正在尋找答案的你
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。