通過Session案例分析一次性驗證碼登錄
驗證碼的實現(xiàn)原理:
在一個Servlet中生成驗證,并把驗證碼上的數(shù)據(jù)保存在Session,用戶提交驗證碼之后,會提交給另外一個Servlet程序。在獲取用戶提交數(shù)據(jù)的Servlet中的從Session中把驗證碼取出,在取出的同時從Session中把驗證碼刪除。
1.注冊頁面:register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> <script type="text/javascript"> function _changImg(){ document.getElementById("myimg").src = "/day11/checkImg?" + new Date().getMilliseconds(); } </script> </head> <body> <center> <form action="/day11/regist" method="post"> <table> <tr> <td>用戶名<font color="red">*</font></td> <td><input type="text" name="name"/></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="password"/></td> </tr> <tr> <td>輸入驗證碼</td> <td> <input type="text" name="form_checkcode" /> <img style="cursor: pointer;" alt="" src="/day11/checkImg" id="myimg" onclick="_changImg();"/> <a href="javascript:void(0);" rel="external nofollow" onclick="_changImg();">看不清,換一張</a> <font color="red">${imgError }</font> </td> </tr> <tr> <td><input type="submit" value="注冊" /></td> </tr> </table> </form> </center> </body> </html>
2.生成驗證碼參考:cn.itcast.session.CheckImgServlet
public class CheckImgServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width = 120; int height = 40; // 先生成一張紙 BufferedImage bufi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 畫筆 Graphics g = bufi.getGraphics(); // 設(shè)置背景顏色 // 修改畫筆顏色 g.setColor(Color.white); // 填充 g.fillRect(0, 0, width, height); // 繪制邊框 // 設(shè)置邊框顏色 g.setColor(Color.red); // 畫邊框 g.drawRect(0, 0, width - 1, height - 1); // 準備一些數(shù)據(jù) String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"; // 生成隨機對象 Random r = new Random(); String checkcode = ""; // 生成4個隨機的數(shù)字 for (int i = 0; i < 4; i++) { // 生成隨機顏色 g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); // 設(shè)置字體 g.setFont(new Font("宋體", Font.ITALIC, 25)); String c = data.charAt(r.nextInt(data.length())) + ""; g.drawString(c, 10 + (20 * i), 30); checkcode += c; } // 將生成的驗證碼放到 session中 request.getSession().setAttribute("session_checkcode", checkcode); // 畫干擾線 for (int i = 0; i < 8; i++) { // 設(shè)置隨機顏色 g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); // 畫線 兩點確定一線 g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)); } // 將圖片輸出到瀏覽器 ImageIO.write(bufi, "jpg", response.getOutputStream()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
3.驗證碼的驗證參考:cn.itcast.session.RegistServlet
public class RegistServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 校驗驗證碼的有效性: 服務(wù)端生成的驗證碼 和 表單提交的驗證碼一致才算有效 // 服務(wù)端生成的驗證碼 保存在 session容器中 // 獲得session中的驗證碼 HttpSession session = request.getSession(); String session_checkcode = (String) session.getAttribute("session_checkcode"); // 使用完后,迅速清除session中驗證碼的屬性 session.removeAttribute("session_checkcode"); // 獲得表單提交的驗證碼 String form_checkcode = request.getParameter("form_checkcode"); // 判斷什么是非法 如果非法,終止 if (session_checkcode == null || !session_checkcode.equals(form_checkcode)) { // 說明驗證碼錯誤 request.setAttribute("imgError", "驗證碼錯誤!"); // 將request對象轉(zhuǎn)發(fā)給 login.jsp request.getRequestDispatcher("/login.jsp").forward(request, response); // 結(jié)束當(dāng)前方法 return; } // 如果合法, 繼續(xù)校驗用戶名和密碼的有效 String username = request.getParameter("username"); String password = request.getParameter("password"); } }
以上所述是小編給大家介紹的通過Session案例分析一次性驗證碼登錄問題,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
JavaWeb項目中springmvc和tomcat對靜態(tài)文件的處理
這篇文章主要介紹了JavaWeb項目中springmvc和tomcat對靜態(tài)文件的處理 的相關(guān)資料,需要的朋友可以參考下2016-07-07springboot使用redis對單個對象進行自動緩存更新刪除的實現(xiàn)
本文主要介紹了springboot使用redis對單個對象進行自動緩存更新刪除的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08解決Spring或SpringBoot開啟事務(wù)以后無法返回自增主鍵的問題
這篇文章主要介紹了解決Spring或SpringBoot開啟事務(wù)以后無法返回自增主鍵的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java多線程中的ReentrantLock可中斷鎖詳細解讀
這篇文章主要介紹了Java多線程中的ReentrantLock可中斷鎖詳細解讀,ReentrantLock中的lockInterruptibly()方法使得線程可以在被阻塞時響應(yīng)中斷,比如一個線程t1通過lockInterruptibly()方法獲取到一個可重入鎖,并執(zhí)行一個長時間的任務(wù),需要的朋友可以參考下2023-12-12