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

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

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

    隨筆-208  評論-469  文章-30  trackbacks-0
    在開發中驗證碼是比較常用到有效防止這種問題對某一個特定注冊用戶用特定程序暴力破解方式進行不斷的登陸嘗試的方式。
    此演示程序包括三個文件:
    1.index.jsp:登錄頁面
    2.image.jsp:生成驗證碼圖片頁面
    3.result.jsp:結果頁面
    【頁面顯示】
    哦哦D74ED2}0.jpg

    【頁面代碼】
    1.index.jsp
    html代碼
    1. <html><body>??????
    2. <formmethod=postaction="result.jsp">??????
    3. <inputtype=textname=inputmaxlength=4>??????
    4. <imgborder=0src="image.jsp">??????
    5. <inputtype="submit"value="submit">??????
    6. </form></body></html>??
    [注意]:
    (1)使用maxlength屬性來限制輸入字符;
    (2)使用<img>標簽來顯示生成的驗證碼圖片.
    2.image.jsp
      1. <%@ page contentType="image/JPEG"??????
      2. ????import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"??????
      3. ????? pageEncoding="GBK"%><%!Color getRandColor(int fc, int bc) {//給定范圍獲得隨機顏色???????
      4. ????????? Random random = new Random();???????
      5. ????????if (fc > 255)???????
      6. ????????????? fc = 255;???????
      7. ????????if (bc > 255)???????
      8. ????????????? bc = 255;???????
      9. ????????int r = fc + random.nextInt(bc - fc);???????
      10. ????????int g = fc + random.nextInt(bc - fc);???????
      11. ????????int b = fc + random.nextInt(bc - fc);???????
      12. ????????returnnew Color(r, g, b);???????
      13. ????? }%><%???????
      14. ????//設置頁面不緩存???????
      15. ????? response.setHeader("Pragma", "No-cache");???????
      16. ????? response.setHeader("Cache-Control", "no-cache");???????
      17. ????? response.setDateHeader("Expires", 0);???????
      18. ??????
      19. ????// 在內存中創建圖象???????
      20. ????int width = 60, height = 20;???????
      21. ????? BufferedImage image = new BufferedImage(width, height,???????
      22. ????????????? BufferedImage.TYPE_INT_RGB);???????
      23. ??????
      24. ????// 獲取圖形上下文???????
      25. ????? Graphics g = image.getGraphics();???????
      26. ??????
      27. ????//生成隨機類???????
      28. ????? Random random = new Random();???????
      29. ??????
      30. ????// 設定背景色???????
      31. ????? g.setColor(getRandColor(200, 250));???????
      32. ????? g.fillRect(0, 0, width, height);???????
      33. ??????
      34. ????//設定字體???????
      35. ????? g.setFont(new Font("Times New Roman", Font.PLAIN, 18));???????
      36. ??????
      37. ????//畫邊框???????
      38. ????//g.setColor(new Color());???????
      39. ????//g.drawRect(0,0,width-1,height-1);???????
      40. ??????
      41. ????// 隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到???????
      42. ????? g.setColor(getRandColor(160, 200));???????
      43. ????for (int i = 0; i < 100; i++) {???????
      44. ????????int x = random.nextInt(width);???????
      45. ????????int y = random.nextInt(height);???????
      46. ????????int xl = random.nextInt(12);???????
      47. ????????int yl = random.nextInt(12);???????
      48. ????????? g.drawLine(x, y, x + xl, y + yl);???????
      49. ????? }???????
      50. ??????
      51. ????// 取隨機產生的認證碼(4位數字)???????
      52. ????? String sRand = "";???????
      53. ????for (int i = 0; i < 4; i++) {???????
      54. ????????? String rand = String.valueOf(random.nextInt(10));???????
      55. ????????? sRand += rand;???????
      56. ????????// 將認證碼顯示到圖象中???????
      57. ????????? g.setColor(new Color(20 + random.nextInt(110), 20 + random???????
      58. ????????? .nextInt(110), 20 + random.nextInt(110)));//調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成???????
      59. ????????? g.drawString(rand, 13 * i + 6, 16);???????
      60. ????? }???????
      61. ??????
      62. ????// 將認證碼存入SESSION???????
      63. ????? session.setAttribute("code", sRand);???????
      64. ??????
      65. ????// 圖象生效???????
      66. ????? g.dispose();???????
      67. ??????
      68. ????// 輸出圖象到頁面???????
      69. ????? ImageIO.write(image, "JPEG", response.getOutputStream());???????
      70. %>??
      ??
    [注意]:
    (1)contentType值設置為"image/JPEG"
    3.result.jsp
    1. <%@ page language="java"import="java.util.*"pageEncoding="GBK"%>??
    2. <html><body>??
    3. <%???
    4. ????? String input=request.getParameter("input");???
    5. ????? String code=(String)session.getAttribute("code");???????
    6. ????? if(input.equals(code)){???
    7. ????????? out.println("驗證成功!");???
    8. ????? }else{???
    9. ????????? out.println("驗證失敗!");???
    10. ????? }???
    11. %>??
    12. </body></html>??
    posted on 2007-04-18 23:38 EricWong 閱讀(353) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲乱亚洲乱妇24p| 亚洲欧洲校园自拍都市| 18禁亚洲深夜福利人口| 亚洲国产另类久久久精品黑人| 风间由美在线亚洲一区| 高清国语自产拍免费视频国产| 天堂亚洲国产中文在线| 国产青草视频在线观看免费影院| 亚洲精品无码专区久久| 四虎影视永久免费观看| 一级毛片免费不卡| 亚洲国产一二三精品无码| 免费91最新地址永久入口| 亚洲最大的成网4438| 桃子视频在线观看高清免费完整| 亚洲入口无毒网址你懂的| 日韩免费毛片视频| ssswww日本免费网站片| 亚洲αv在线精品糸列| 精品熟女少妇a∨免费久久| 国产99在线|亚洲| 免费一区二区三区四区五区| 成年免费大片黄在线观看com| 亚洲中文字幕无码爆乳AV| 精品熟女少妇a∨免费久久| 亚洲欧美一区二区三区日产| 亚洲福利中文字幕在线网址| a毛片免费全部在线播放**| 亚洲欧洲国产成人精品| 国产成人免费a在线资源| a级毛片黄免费a级毛片| 亚洲一区二区三区91| 亚洲精品一级无码中文字幕| 亚洲电影免费在线观看| 亚洲av永久中文无码精品综合| 亚洲中文字幕成人在线| 免费观看国产网址你懂的| 特级av毛片免费观看| 久久亚洲精精品中文字幕| 看全色黄大色大片免费久久 | 亚洲最大av资源站无码av网址|