一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 通過Session案例分析一次性驗證碼登錄

通過Session案例分析一次性驗證碼登錄

2020-08-24 10:39wearetheworld1 Java教程

這篇文章主要介紹了通過Session案例分析一次性驗證碼登錄,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

 驗證碼的實現原理:

在一個Servlet中生成驗證,并把驗證碼上的數據保存在Session,用戶提交驗證碼之后,會提交給另外一個Servlet程序。在獲取用戶提交數據的Servlet中的從Session中把驗證碼取出,在取出的同時從Session中把驗證碼刪除。

1.注冊頁面:register.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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();
    // 設置背景顏色
    // 修改畫筆顏色
    g.setColor(Color.white);
    // 填充
    g.fillRect(0, 0, width, height);
    // 繪制邊框
    // 設置邊框顏色
    g.setColor(Color.red);
    // 畫邊框
    g.drawRect(0, 0, width - 1, height - 1);
    // 準備一些數據
    String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
    // 生成隨機對象
    Random r = new Random();
    String checkcode = "";
    // 生成4個隨機的數字
    for (int i = 0; i < 4; i++) {
      // 生成隨機顏色
      g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
      // 設置字體
      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++) {
      // 設置隨機顏色
      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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 {
    // 校驗驗證碼的有效性: 服務端生成的驗證碼 和 表單提交的驗證碼一致才算有效
    // 服務端生成的驗證碼 保存在 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對象轉發給 login.jsp
      request.getRequestDispatcher("/login.jsp").forward(request, response);
      // 結束當前方法
      return;
    }
    // 如果合法, 繼續校驗用戶名和密碼的有效
    String username = request.getParameter("username");
    String password = request.getParameter("password");
  }
}

以上所述是小編給大家介紹的通過Session案例分析一次性驗證碼登錄問題,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

原文鏈接:http://blog.csdn.net/wearetheworld1/article/details/60464062

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品一区二区国产 | 日日摸夜夜爽色婷婷91 | 黑人粗长大战亚洲女 | 国产第一页无线好源 | 506070老熟肥妇bbwxx视频 500第一精品 | 校园纯肉H教室第一次 | 亚洲精品九色在线网站 | bl双性肉文 | 日韩视频免费一区二区三区 | 色老太bbbbb| 99精品久久精品一区二区 | 动漫美女强行被吸乳做羞羞事 | 亚洲 欧美 另类 中文 在线 | 亚洲视频中文字幕 | 99久久精品久久久久久清纯 | 四虎成人永久地址 | 98pao强力打造高清免费 | 美女扒开胸罩露出胸大乳 | 国产精品日韩欧美一区二区三区 | 91av免费在线观看 | 操弄哥哥的108种姿势 | 国产精品调教 | 欧美va免费大片 | 成人一区二区免费中文字幕 | 日韩一区在线观看 | 婷婷中文 | 国产福利兔女郎在线观看 | 喘息揉弄1v1h老师 | 久久99re热在线播放7 | 青柠影视在线播放观看高清 | 亚洲精品tv久久久久久久久久 | 国产精品亚洲午夜一区二区三区 | 极品美女写真菠萝蜜视频 | 国产资源在线视频 | 亚洲人成网站在线观看青青 | 国产日本久久久久久久久婷婷 | 蹭蹭妈妈的朋友小说 | 日本视频一区在线观看免费 | 91免费精品国自产拍在线不卡 | 成人观看免费大片在线观看 | 国产在线观看福利 |