<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    添加動態驗證碼

    Posted on 2007-07-25 14:26 胡娟 閱讀(716) 評論(1)  編輯  收藏
    1.用戶信息VO包
     1package com.hujuan.VO;
     2
     3public class UserInfo {
     4    private String username;
     5    private String password;
     6    public String getPassword() {
     7        return password;
     8    }

     9    public void setPassword(String password) {
    10        this.password = password;
    11    }

    12    public String getUsername() {
    13        return username;
    14    }

    15    public void setUsername(String username) {
    16        this.username = username;
    17    }

    18}
    2.驗證登錄Dao包
     1package com.hujuan.dao;
     2
     3import com.hujuan.VO.UserInfo;
     4
     5public class UserInfoDao {
     6    public boolean checklogin(UserInfo userinfo){
     7        System.out.println(userinfo.getUsername());
     8        System.out.println(userinfo.getUsername());
     9        if("hujuan".equals(userinfo.getUsername())&&"123".equals(userinfo.getPassword())){
    10            return true;
    11        }
    return false;
    12    }

    13}
    3.生產隨機碼驗證servlet
     1package com.hujuan.servlet;
     2
     3
     4import java.awt.Graphics2D;
     5import java.awt.image.BufferedImage;
     6import java.io.IOException;
     7import java.util.Random;
     8import java.awt.Color;
     9import java.awt.Font;
    10import javax.imageio.ImageIO;
    11import javax.servlet.ServletException;
    12import javax.servlet.ServletOutputStream;
    13import javax.servlet.http.HttpServlet;
    14import javax.servlet.http.HttpServletRequest;
    15import javax.servlet.http.HttpServletResponse;
    16import javax.servlet.http.HttpSession;
    17
    18public class RandomCodeServler extends HttpServlet {
    19    private int width = 60;    //驗證圖片的寬度
    20    private int height = 20//驗證圖片的高度
    21    @Override
    22    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    23        
    24        BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    25        
    26        Graphics2D g = buffImg.createGraphics();
    27    
    28    
    29    //創建一個隨機數生產器類
    30    Random random = new Random();
    31    
    32    g.setColor(Color.WHITE);
    33    g.fillRect(00, width, height);
    34    
    35    //創建字體,字體的大小應該根據圖片的高度來定
    36    Font font = new Font("123123",Font.PLAIN,18);
    37    //設置字體
    38    g.setFont(font);
    39    //畫邊框
    40    g.setColor(Color.BLACK);
    41    g.drawRect(00, width-1,height-1);
    42    //隨機生產100條干擾線,使圖像中的驗證嗎不易被其他程序探測到
    43    g.setColor(Color.GRAY);
    44    for(int i=0;i<100;i++){
    45        int x = random.nextInt(width);
    46        int y = random.nextInt(height);
    47        int x1 = random.nextInt(12);
    48        int y1 = random.nextInt(12);
    49        g.drawLine(x, y, x+x1, y+y1);
    50    }

    51    //randomCode 用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證
    52    StringBuffer randomCode = new StringBuffer();
    53    int red = 0,green = 0,blue = 0;
    54    //隨機產生4位數字的驗證碼
    55    for(int i = 0;i<4;i++){
    56        //得到隨機產生的驗證碼數字
    57        String strRand = String.valueOf(random.nextInt(10));
    58        
    59        //產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同
    60        red = random.nextInt(110);
    61        green = random.nextInt(50);
    62        blue = random.nextInt(50);
    63        
    64        //用隨機產生的顏色將驗證碼繪制到圖像中
    65        g.setColor(new Color(red,green,blue));
    66        g.drawString(strRand, 13*i+616);
    67        //將產生的四個隨機數組合在一起
    68        randomCode.append(strRand);
    69    }

    70    //將四位數字的驗證碼保存到session中
    71    HttpSession session = req.getSession();
    72    session.setAttribute("randomCode", randomCode.toString());
    73    //禁止圖像緩存
    74//    resp.setHeader("Pragma", "no-cache");
    75//    resp.setHeader("Cache-Control", "no-cache");
    76//    resp.setDateHeader("Expires", 0);
    77//    resp.setContentType("image/jpeg");
    78    //將圖像輸出到servlet輸出流中
    79    ServletOutputStream out = resp.getOutputStream();
    80    ImageIO.write(buffImg,"jpeg",out);
    81    out.close();
    82    }

    83}

    84
    4.驗證登錄的servlet
     1package com.hujuan.servlet;
     2
     3import java.io.IOException;
     4import java.io.PrintWriter;
     5import javax.servlet.ServletException;
     6import javax.servlet.http.HttpServlet;
     7import javax.servlet.http.HttpServletRequest;
     8import javax.servlet.http.HttpServletResponse;
     9import javax.servlet.http.HttpSession;
    10import com.hujuan.VO.UserInfo;
    11import com.hujuan.dao.UserInfoDao;
    12
    13public class LoginCheckServlet extends HttpServlet {
    14
    15    public void doPost(HttpServletRequest request, HttpServletResponse response)
    16            throws ServletException, IOException {
    17
    18        //request.setCharacterEncoding("gb2312");
    19        response.setCharacterEncoding("gb2312");
    20        
    21        response.setContentType("text/html; charset = gb2312");
    22        PrintWriter out = response.getWriter();
    23        
    24        HttpSession session = request.getSession();
    25        String randomCode =(String)session.getAttribute("randomCode");
    26        
    27        if(null == randomCode){
    28            response.sendRedirect("index.jsp");
    29            return;
    30        }

    31        
    32        String reqRandom = request.getParameter("randomcode");//meiyou huode
    33
    34        UserInfo userinfo = new UserInfo();
    35        String name = request.getParameter("username");
    36        String password = request.getParameter("password");
    37        
    38        userinfo.setUsername(name);
    39        userinfo.setPassword(password);
    40        
    41        
    42//        System.out.println(reqRandom);
    43//        System.out.println(randomCode);
    44//        System.out.println(name);
    45//        System.out.println(password);
    46        if(randomCode.equals(reqRandom)){
    47            UserInfoDao userdao = new UserInfoDao();
    48            if(userdao.checklogin(userinfo)){
    49                response.sendRedirect("succ.jsp");
    50            }
    else{
    51                out.println("請檢查用戶名和密碼");
    52            }

    53        }
    else{
    54            out.println("請檢查驗證碼");
    55        }

    56    }

    57
    58}

    59
    5.登錄頁面
     1<%@ page language="java" import="java.util.*" 
     2    contentType="text/html; charset=gb2312"%>
     3<%
     4    String path = request.getContextPath();
     5    String basePath = request.getScheme() + "://"
     6            + request.getServerName() + ":" + request.getServerPort()
     7            + path + "/";
     8%>
     9
    10<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    11<html>
    12    <head>
    13        <base href="<%=basePath%>">
    14
    15        <title>My JSP 'Login.jsp' starting page</title>
    16
    17        <meta http-equiv="pragma" content="no-cache">
    18        <meta http-equiv="cache-control" content="no-cache">
    19        <meta http-equiv="expires" content="0">
    20        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    21        <meta http-equiv="description" content="This is my page">
    22        <!--
    23    <link rel="stylesheet" type="text/css" href="styles.css">
    24    -->
    25
    26        <style type="text/css">
    27<!--
    28.text {
    29    font-family: "宋體";
    30    font-size: 12px;
    31    font-style: normal;
    32    line-height: 25px;
    33    font-weight: normal;
    34    font-variant: normal;
    35    color: #000000;
    36}

    37-->
    38        </style>
    39</head>
    40
    41    <body>
    42        <form method="post" name="login" action="LoginCheckServlet">
    43            <p>&nbsp;
    44                
    45          </p>
    46            <table width="40%" border="0" align="center">
    47              <tbody>
    48                <tr>
    49                  <td width="22%" align="right" class="text">&nbsp;用戶名 </td>
    50                  <td width="37%"><input name="username" type="text" size="20">
    51                      <br>
    52                  </td>
    53                </tr>
    54                <tr>
    55                  <td align="right" class="text">&nbsp;密 碼 </td>
    56                  <td><input name="password" type="password" size="20" maxlength="16">
    57                  </td>
    58                </tr>
    59                <tr>
    60                  <td align="right" class="text">&nbsp;驗證碼 </td>
    61                  <td><input name="randomcode" type="text" size="5" maxlength="4">
    62                      <img src="imgcode"> </td>
    63                </tr>
    64                <tr>
    65                  <td align="right">&nbsp;
    66                  <input type="submit" value="提交" name="submit">                  </td>
    67                  <td align="center">&nbsp;
    68                      <input type="reset" value="重置" name="reset">                  </td>
    69                </tr>
    70              </tbody>
    71          </table>
    72        </form>
    73    </body>
    74</html>
    75
    6.成功頁面
     1<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2<%
     3String path = request.getContextPath();
     4String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5%>
     6
     7<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8<html>
     9  <head>
    10    <base href="<%=basePath%>">
    11    
    12    <title>My JSP 'succ.jsp' starting page</title>
    13    
    14    <meta http-equiv="pragma" content="no-cache">
    15    <meta http-equiv="cache-control" content="no-cache">
    16    <meta http-equiv="expires" content="0">    
    17    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18    <meta http-equiv="description" content="This is my page">
    19    <!--
    20    <link rel="stylesheet" type="text/css" href="styles.css">
    21    -->
    22
    23  </head>
    24  
    25  <body>
    26    This is my JSP page. <br>
    27  </body>
    28</html>
    29

    Feedback

    # re: 添加動態驗證碼  回復  更多評論   

    2012-05-27 22:04 by mable
    我覺得很奇怪,為什么我的驗證碼總是“62”呢?勞煩給我解釋一下servlet繪制的驗證碼圖片是怎么放在登陸頁面的啊? <img src="imgcode">,imgcode從哪里來?

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    posts - 28, comments - 5, trackbacks - 0, articles - 1

    Copyright © 胡娟

    主站蜘蛛池模板: 国产片AV片永久免费观看| 亚洲精品中文字幕无码AV| 色婷婷7777免费视频在线观看| 亚洲精品国产日韩无码AV永久免费网 | 亚洲日本一区二区三区在线| 免费无码又爽又刺激毛片| 亚洲精品在线免费看| 最近国语视频在线观看免费播放| 亚洲欧美日韩中文字幕一区二区三区 | 亚洲爽爽一区二区三区| 青青青青青青久久久免费观看| h视频在线观看免费完整版| 中文无码成人免费视频在线观看| 四虎影视在线看免费观看| 亚洲av无码专区青青草原| 亚洲一区二区三区免费观看 | 免费在线看黄的网站| 色吊丝性永久免费看码| 黄色a三级免费看| 亚洲av无码成人影院一区 | 成年私人影院免费视频网站| 99热在线观看免费| 一个人免费日韩不卡视频| 精品国产污污免费网站| 国产色无码精品视频免费| 国产精品免费久久久久久久久 | 亚洲一级黄色视频| 亚洲麻豆精品国偷自产在线91| 亚洲A∨午夜成人片精品网站| 国产免费午夜a无码v视频| 国产精品va无码免费麻豆| 日本免费一本天堂在线| 精品免费国产一区二区| 岛国片在线免费观看| 午夜精品在线免费观看| 好爽好紧好大的免费视频国产| 日本黄页网站免费| 亚洲 综合 国产 欧洲 丝袜| 国产极品美女高潮抽搐免费网站| 国产三级电影免费观看| 久久久久亚洲av毛片大|