欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過(guò)Session案例分析一次性驗(yàn)證碼登錄

 更新時(shí)間:2017年03月05日 10:26:01   作者:wearetheworld1  
這篇文章主要介紹了通過(guò)Session案例分析一次性驗(yàn)證碼登錄,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

 驗(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)文章

最新評(píng)論