通過(guò)Session案例分析一次性驗(yàn)證碼登錄
驗(yàn)證碼的實(shí)現(xiàn)原理:
在一個(gè)Servlet中生成驗(yàn)證,并把驗(yàn)證碼上的數(shù)據(jù)保存在Session,用戶提交驗(yàn)證碼之后,會(huì)提交給另外一個(gè)Servlet程序。在獲取用戶提交數(shù)據(jù)的Servlet中的從Session中把驗(yàn)證碼取出,在取出的同時(shí)從Session中把驗(yàn)證碼刪除。
1.注冊(cè)頁(yè)面: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>輸入驗(yàn)證碼</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="注冊(cè)" /></td> </tr> </table> </form> </center> </body> </html>
2.生成驗(yàn)證碼參考: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); // 準(zhǔn)備一些數(shù)據(jù) String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"; // 生成隨機(jī)對(duì)象 Random r = new Random(); String checkcode = ""; // 生成4個(gè)隨機(jī)的數(shù)字 for (int i = 0; i < 4; i++) { // 生成隨機(jī)顏色 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; } // 將生成的驗(yàn)證碼放到 session中 request.getSession().setAttribute("session_checkcode", checkcode); // 畫干擾線 for (int i = 0; i < 8; i++) { // 設(shè)置隨機(jī)顏色 g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); // 畫線 兩點(diǎn)確定一線 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.驗(yàn)證碼的驗(yàn)證參考: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 { // 校驗(yàn)驗(yàn)證碼的有效性: 服務(wù)端生成的驗(yàn)證碼 和 表單提交的驗(yàn)證碼一致才算有效 // 服務(wù)端生成的驗(yàn)證碼 保存在 session容器中 // 獲得session中的驗(yàn)證碼 HttpSession session = request.getSession(); String session_checkcode = (String) session.getAttribute("session_checkcode"); // 使用完后,迅速清除session中驗(yàn)證碼的屬性 session.removeAttribute("session_checkcode"); // 獲得表單提交的驗(yàn)證碼 String form_checkcode = request.getParameter("form_checkcode"); // 判斷什么是非法 如果非法,終止 if (session_checkcode == null || !session_checkcode.equals(form_checkcode)) { // 說(shuō)明驗(yàn)證碼錯(cuò)誤 request.setAttribute("imgError", "驗(yàn)證碼錯(cuò)誤!"); // 將request對(duì)象轉(zhuǎn)發(fā)給 login.jsp request.getRequestDispatcher("/login.jsp").forward(request, response); // 結(jié)束當(dāng)前方法 return; } // 如果合法, 繼續(xù)校驗(yàn)用戶名和密碼的有效 String username = request.getParameter("username"); String password = request.getParameter("password"); } }
以上所述是小編給大家介紹的通過(guò)Session案例分析一次性驗(yàn)證碼登錄問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Java與MySQL導(dǎo)致的時(shí)間不一致問(wèn)題分析
在使用MySQL的過(guò)程中,你可能會(huì)遇到時(shí)區(qū)相關(guān)問(wèn)題,本文主要介紹了Java與MySQL導(dǎo)致的時(shí)間不一致問(wèn)題分析,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07JavaWeb項(xiàng)目中springmvc和tomcat對(duì)靜態(tài)文件的處理
這篇文章主要介紹了JavaWeb項(xiàng)目中springmvc和tomcat對(duì)靜態(tài)文件的處理 的相關(guān)資料,需要的朋友可以參考下2016-07-07springboot使用redis對(duì)單個(gè)對(duì)象進(jìn)行自動(dòng)緩存更新刪除的實(shí)現(xiàn)
本文主要介紹了springboot使用redis對(duì)單個(gè)對(duì)象進(jìn)行自動(dòng)緩存更新刪除的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Mybatis中使用updateBatch進(jìn)行批量更新
這篇文章主要介紹了Mybatis中使用updateBatch進(jìn)行批量更新的相關(guān)資料,有逐條更新,sql批量更新等,具體實(shí)例代碼大家參考下本文2018-04-04解決Spring或SpringBoot開啟事務(wù)以后無(wú)法返回自增主鍵的問(wèn)題
這篇文章主要介紹了解決Spring或SpringBoot開啟事務(wù)以后無(wú)法返回自增主鍵的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07MyBatis超詳細(xì)講解如何實(shí)現(xiàn)分頁(yè)功能
MyBatis-Plus?是一個(gè)?Mybatis?增強(qiáng)版工具,在?MyBatis?上擴(kuò)充了其他功能沒有改變其基本功能,為了簡(jiǎn)化開發(fā)提交效率而存在,本篇文章帶用它實(shí)現(xiàn)分頁(yè)功能2022-03-03Java多線程中的ReentrantLock可中斷鎖詳細(xì)解讀
這篇文章主要介紹了Java多線程中的ReentrantLock可中斷鎖詳細(xì)解讀,ReentrantLock中的lockInterruptibly()方法使得線程可以在被阻塞時(shí)響應(yīng)中斷,比如一個(gè)線程t1通過(guò)lockInterruptibly()方法獲取到一個(gè)可重入鎖,并執(zhí)行一個(gè)長(zhǎng)時(shí)間的任務(wù),需要的朋友可以參考下2023-12-12