<!-- Feedsky FEED发布代码开始 -->
如果您喜欢这些文章,欢迎点击此处订阅本Blog
<!-- FEED自动发现标记开始 --> <link title="RSS 2.0" type="application/rss+xml" href="http://feed.feedsky.com/softwave" rel="alternate"> <!-- FEED自动发现标记结束 -->
<!--Google 468*60横幅广告开始--><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横幅广告结束-->
本文将介绍有关JSP中的一种验证码的实现,即中文验证码在JSP中的实现,使用中文验证码的好处是能提高验证的有效性,提高验证的安全度,因为中文相对于英文或数字笔画结构相对比较复杂,从而增加了分析程序解析验证码图片并读取验证信息的难度。在文中并分析中文验证的实现过程。
JSP中实现中文验证码源码如下:
以上就是JSP中产生中文验证码过程的源码分析,由中文作为验证码存在于JSP页面中,将会存在JSP中文乱码的现象,接下来文章将介绍如何调用JSP验证码产生和使用的方法,以及如何解决JSP中文验证码乱码问题和其他JAVA验证码的实现。
自【 http://www.blogjava.net/improviser/archive/2008/11/29/243370.html 】
<!--新Google 468*60横幅广告开始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 创建于 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横幅广告结束-->
<!--新Google 468x15 横链接单元开始--><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 横链接单元结束-->
<!-- Google Reader shared发布代码开始 --><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发布代码结束 -->
<!--Google 468*60横幅广告开始--><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横幅广告结束-->
本文将介绍有关JSP中的一种验证码的实现,即中文验证码在JSP中的实现,使用中文验证码的好处是能提高验证的有效性,提高验证的安全度,因为中文相对于英文或数字笔画结构相对比较复杂,从而增加了分析程序解析验证码图片并读取验证信息的难度。在文中并分析中文验证的实现过程。
JSP中实现中文验证码源码如下:
- <!--ChineseVal.jsp-->
-
- <%@pagepageEncoding= "gb2312" contentType= "image/jpeg" import = "javax.imageio.*,java.util.*,java.awt.image.*,java.awt.*" %>
-
- <%!
-
- //在此处获取并生成随机颜色
-
- 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);
-
- }%>
-
- <%
-
- //在此处设置JSP页面无缓存
-
- response.setHeader( "Pragma" , "No-cache" );
-
- response.setHeader( "Cache-Control" , "no-cache" );
-
- response.setDateHeader( "Expires" , 0 );
-
- //设置图片的长宽
-
- int width= 130 ;
- int height= 30 ;
-
- //设定被随机选取的中文字,此处中文字内容过多,不一一列出,只是举例说明下。
- 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...省略文字。。。" ;
-
- //设置备选随机汉字的个数
-
- int length=base.length();
-
- //创建缓存图像
-
- BufferedImageimage= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
-
- //获取图像
- Graphicsg=image.getGraphics();
-
- //创建随机函数的实例
-
- Randomrandom= new Random();
-
- //此处设定图像背景色
-
- g.setColor(getRandColor(random, 188 , 235 ));
-
- g.fillRect( 0 , 0 ,width,height);
-
- //设置随机备选的字体类型
-
- String[]fontTypes={ "/u5b8b/u4f53" , "/u65b0/u5b8b/u4f53" ,
-
- "/u9ed1/u4f53" , "/u6977/u4f53" , "/u96b6/u4e66" };
-
- int fontTypesLength=fontTypes.length;
-
- //在图片背景上增加噪点,增加图片分析难度
-
- 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 ));
-
- }
-
- //取随机产生的验证码(4个汉字)
-
- //保存生成的汉字字符串
-
- StringsRand= "" ;
-
- for ( int i= 0 ;i< 4 ;i++){
-
- int start=random.nextInt(length);
-
- Stringrand=base.substring(start,start+ 1 );
-
- sRand+=rand;
-
- //设置图片上字体的颜色
-
- g.setColor(getRandColor(random, 10 , 150 ));
-
- //设置字体格式
-
- g.setFont( new Font(fontTypes[random.nextInt(fontTypesLength)],
-
- Font.BOLD, 18 +random.nextInt( 6 )));
-
- //将此汉字画到验证图片上面
-
- g.drawString(rand, 24 *i+ 10 +random.nextInt( 8 ), 24 );
-
- }
-
- //将验证码存入Session中
-
- session.setAttribute( "rand" ,sRand);
-
- g.dispose();
-
- //将图象输出到JSP页面中
-
- ImageIO.write(image, "JPEG" ,response.getOutputStream());
-
- //关闭流
-
- out.clear();
-
- out=pageContext.pushBody();
-
- %>
自【 http://www.blogjava.net/improviser/archive/2008/11/29/243370.html 】
<!--新Google 468*60横幅广告开始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 创建于 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横幅广告结束-->
<!--新Google 468x15 横链接单元开始--><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 横链接单元结束-->
<!-- Google Reader shared发布代码开始 --><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发布代码结束 -->