
<!--Google 468*60橫幅廣告開(kāi)始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--Google 468*60橫幅廣告結(jié)束-->
本文將介紹有關(guān)JSP中的一種驗(yàn)證碼的實(shí)現(xiàn),即中文驗(yàn)證碼在JSP中的實(shí)現(xiàn),使用中文驗(yàn)證碼的好處是能提高驗(yàn)證的有效性,提高驗(yàn)證的安全度,因?yàn)橹形南鄬?duì)于英文或數(shù)字筆畫(huà)結(jié)構(gòu)相對(duì)比較復(fù)雜,從而增加了分析程序解析驗(yàn)證碼圖片并讀取驗(yàn)證信息的難度。在文中并分析中文驗(yàn)證的實(shí)現(xiàn)過(guò)程。
JSP中實(shí)現(xiàn)中文驗(yàn)證碼源碼如下:
- <!--ChineseVal.jsp-->
-
- <%@pagepageEncoding= "gb2312" contentType= "image/jpeg" import = "javax.imageio.*,java.util.*,java.awt.image.*,java.awt.*" %>
-
- <%!
-
- //在此處獲取并生成隨機(jī)顏色
-
- ColorgetRandColor(Randomrandom, int ff, int cc){
-
- if (ff> 255 )
-
- ff= 255 ;
-
- if (cc> 255 )
-
- cc= 255 ;
-
- int r=ff+random.nextInt(cc-ff);
-
- int g=ff+random.nextInt(cc-ff);
-
- int b=ff+random.nextInt(cc-ff);
-
- return new Color(r,g,b);
-
- }%>
-
- <%
-
- //在此處設(shè)置JSP頁(yè)面無(wú)緩存
-
- response.setHeader( "Pragma" , "No-cache" );
-
- response.setHeader( "Cache-Control" , "no-cache" );
-
- response.setDateHeader( "Expires" , 0 );
-
- //設(shè)置圖片的長(zhǎng)寬
-
- int width= 130 ;
- int height= 30 ;
-
- //設(shè)定被隨機(jī)選取的中文字,此處中文字內(nèi)容過(guò)多,不一一列出,只是舉例說(shuō)明下。
- Stringbase= "/u9752/u534a/u706b/u6cd5/u9898/u5efa/u8d76/u4f4d/u5531/u6d77/u4e03/u5973/u4efb/u4ef6/u611f/u51c6/u97f3/u7b54/u54e5/u9645/u65e7/u795e/u5ea7/u7ae0/u538b/u6162/u53d4/u80cc/u7ec6...省略文字。。。" ;
-
- //設(shè)置備選隨機(jī)漢字的個(gè)數(shù)
-
- int length=base.length();
-
- //創(chuàng)建緩存圖像
-
- BufferedImageimage= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
-
- //獲取圖像
- Graphicsg=image.getGraphics();
-
- //創(chuàng)建隨機(jī)函數(shù)的實(shí)例
-
- Randomrandom= new Random();
-
- //此處設(shè)定圖像背景色
-
- g.setColor(getRandColor(random, 188 , 235 ));
-
- g.fillRect( 0 , 0 ,width,height);
-
- //設(shè)置隨機(jī)備選的字體類型
-
- String[]fontTypes={ "/u5b8b/u4f53" , "/u65b0/u5b8b/u4f53" ,
-
- "/u9ed1/u4f53" , "/u6977/u4f53" , "/u96b6/u4e66" };
-
- int fontTypesLength=fontTypes.length;
-
- //在圖片背景上增加噪點(diǎn),增加圖片分析難度
-
- g.setColor(getRandColor(random, 180 , 199 ));
-
- g.setFont( new Font( "TimesNewRoman" ,Font.PLAIN, 14 ));
-
- for ( int i= 0 ;i< 4 ;i++){
-
- g.drawString( "@*@*@*@*@*@*@*" ,
-
- 0 , 5 *(i+ 2 ));
-
- }
-
- //取隨機(jī)產(chǎn)生的驗(yàn)證碼(4個(gè)漢字)
-
- //保存生成的漢字字符串
-
- StringsRand= "" ;
-
- for ( int i= 0 ;i< 4 ;i++){
-
- int start=random.nextInt(length);
-
- Stringrand=base.substring(start,start+ 1 );
-
- sRand+=rand;
-
- //設(shè)置圖片上字體的顏色
-
- g.setColor(getRandColor(random, 10 , 150 ));
-
- //設(shè)置字體格式
-
- g.setFont( new Font(fontTypes[random.nextInt(fontTypesLength)],
-
- Font.BOLD, 18 +random.nextInt( 6 )));
-
- //將此漢字畫(huà)到驗(yàn)證圖片上面
-
- g.drawString(rand, 24 *i+ 10 +random.nextInt( 8 ), 24 );
-
- }
-
- //將驗(yàn)證碼存入Session中
-
- session.setAttribute( "rand" ,sRand);
-
- g.dispose();
-
- //將圖象輸出到JSP頁(yè)面中
-
- ImageIO.write(image, "JPEG" ,response.getOutputStream());
-
- //關(guān)閉流
-
- out.clear();
-
- out=pageContext.pushBody();
-
- %>
自【 http://www.blogjava.net/improviser/archive/2008/11/29/243370.html 】
<!--新Google 468*60橫幅廣告開(kāi)始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 創(chuàng)建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468*60橫幅廣告結(jié)束-->
<!--新Google 468x15 橫鏈接單元開(kāi)始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 橫鏈接單元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468x15 橫鏈接單元結(jié)束-->
<!-- Google Reader shared發(fā)布代碼開(kāi)始 --><script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script><script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p%28%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D%29%3Bnew%20GRC"></script><!-- Google Reader shared發(fā)布代碼結(jié)束 -->
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
