內蒙古java團隊

          j2se,j2ee開發組
          posts - 139, comments - 212, trackbacks - 0, articles - 65
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          JSP驗證碼大全之數字驗證碼[轉]

          Posted on 2010-04-28 09:54 帥子 閱讀(484) 評論(1)  編輯  收藏 所屬分類: J2EE技術專區
          ????? ? 驗證碼應用在各種場合中,十分廣泛,在多種多樣的系統或軟件中的新用戶帳戶注冊、用戶登錄、網站統一登陸或者用戶在網站發布信息模塊發布文章或內容都添加的隨機碼功能,對用戶操作過程進行一種驗證,使用驗證碼的目的就是為了避免網絡中的自動注冊程序或者自動發布程序的濫用。 驗證碼的原理其實就是隨機選擇一些字符碼以將字符碼以圖片的形式展現在軟件驗證界面或驗證頁面上,當用戶在進行提交操作的同時需要將圖片上的驗證碼輸入并同時提交,如果提交的字符驗證碼與服務器session保存的字符碼相同,則認為提交信息有效,否則拒絕提交。在使用驗證碼過程中,為了避免自動分析程序解析圖片并獲得驗證信息,通常需要在圖片上隨機生成一些干擾線或者將復雜的字符對圖片進行扭曲模糊,從而增加了自動識別程序分析驗證圖片的難度。
          ???JSP驗證碼源碼大全將分別用幾個篇幅的內容來介紹在JSP中幾種驗證碼的實現源碼以及使用。
          ???一、JSP中產生數字驗證碼源碼???
          ???數字驗證碼是一種最常用的驗證字符碼形式,以下為數字實現的JSP源碼:
          ???
          Num.jsp
          <%@
          page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>

          <%!

          ??? Color getRandColor(int cc,int bb)

          ??? {

          ??????? Random random = new Random();

          ??????? if(fc>255) cc=255;

          ??????? if(bc>255) bb=255;

          ??????? int r=cc+random.nextInt(bb-cc);

          ??????? int g=cc+random.nextInt(bb-cc);

          ??????? int b=cc+random.nextInt(bb-cc);

          ??????? returnnew Color(r,g,b);

          ??? } //獲取隨機顏色

          %>

          <%

          ??? response.setHeader("Pragma","No-cache");

          ??? response.setHeader("Cache-Control","no-cache");

          ??? response.setDateHeader("Expires", 0);

          ?

          ??? int width=80; //定義驗證碼圖片的長度

          ??? int height=30; //定義驗證碼圖片的寬度

          ??? BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

          ?

          ??? Graphics g = image.getGraphics();

          ?

          ??? Random random = new Random();

          ?

          ??? g.setColor(getRandColor(200,250));

          ??? g.fillRect(0, 0, width, height);

          ?

          ??? g.setFont(new Font("Times New Roman",Font.PLAIN,18));

          ????//定義字體形式
          ?

          ??? g.setColor(getRandColor(160,200));

          ??? for (int i=0;i<155;i++)

          ??? {

          ?????? int i_x = random.nextInt(width);

          ?????? int i_y = random.nextInt(height);

          ?????? int i_xl = random.nextInt(12);

          ?????? int i_yl = random.nextInt(12);

          ?????? g.drawLine(i_x,i_y,i_x+i_xl,i_y+i_yl);

          ??? }

          ??? //用線條畫背景
          ?

          ??? String s_Rand="";

          ??? for (int i=0;i<4;i++)

          ??? {

          ?????? String rand=String.valueOf(random.nextInt(10));

          ?????? s_Rand+=rand;

          ??????

          ?????? g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));

          ?????? g.drawString(rand,13*i+6,16);

          ??? }

          ????//產生4位隨機碼?
          ?

          ??? session.setAttribute("rand",s_Rand);

          ????//將驗證碼存入Session中
          ?

          ??? g.dispose();

          ???

          ??? ImageIO.write(image, "JPEG", response.getOutputStream());

          ????//輸出驗證圖片
          ???

          ??? out.clear();

          ??? out = pageContext.pushBody();

          ???

          %>


          評論

          # re: JSP驗證碼大全之數字驗證碼[轉]  回復  更多評論   

          2011-06-11 01:27 by 蔣文超
          我要玩!
          主站蜘蛛池模板: 青神县| 门头沟区| 象州县| 光山县| 沁阳市| 宜君县| 龙胜| 嘉黎县| 留坝县| 靖远县| 施秉县| 石嘴山市| 兴宁市| 石泉县| 金寨县| 唐山市| 湄潭县| 临汾市| 北宁市| 浪卡子县| 伊吾县| 辽阳市| 广州市| 外汇| 健康| 中方县| 剑川县| 荆州市| 丁青县| 故城县| 阜宁县| 徐汇区| 凤凰县| 平顶山市| 拉萨市| 西林县| 晴隆县| 普宁市| 成武县| 平阳县| 江都市|