采用ajax实现的图片验证码

上传者: lonerma | 上传时间: 2021-04-09 15:02:12 | 文件大小: 443KB | 文件类型: RAR
采用ajax技术实现的图形验证码,在前端进行验证。验证码信息为图片。每一行代码均有注释,通俗易懂。 实现步骤: 1、创建web工程 2、在jsp页面,编写一个 3、编写一个servlet,在servlet中产生图形验证码 ------------------------------------------------------------ //1、给客户端作出的回应是以图片的方式来回应 response.setContentType("image/jpeg"); //2、创建一个图形缓冲区,用于绘制图形 (宽度,高度,颜色的生成方案) BufferedImage image = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB); //3、创建一支画笔(图形设备接口)用于绘图 Graphics g = image.getGraphics(); //4、指定图笔的颜色 g.setColor(getColor(200,256)); //5、绘制一个矩形框,作为验证码的背景 g.fillRect(0,0, 800,600); //产生一个输出流,准备把图片以流的方式,输出到客户端 OutputStream out = response.getOutputStream(); //输出在图形缓冲区中,绘制的图片 ImageIO.write(image,"jpg",out); //关闭流 out.close(); //随机生成背景颜色 private Random rd = new Random(); //产生随机数类 public Color getColor(int start,int end){ int r = start+rd.nextInt(end-start); int g = start+rd.nextInt(end-start); int b = start+rd.nextInt(end-start); return new Color(r, g, b);//根据三原色的值,随机在指定范围内,生成一种颜色 } --------------------------------------------------------------------------- 0-120 比较适合文字的颜色 100-200 适合干扰线条的颜色 200-255 适合背景颜色 --------------------------------------------------------------------------- 生成图片中的文字: 1、先编写一个字符串,包含:数字,大小字母 private String s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 2、在产生背景之后,编写如下代码,产生四个字符(并且,把生成字符,保存在session中,在登录的时候用来做判断) String code=""; //用于保存生成的四个字符 for(int i=0;i<4;i++){ //生成一个随机数,它的取值范围,一定要在s这个字符串的长度范围之内 int index = rd.nextInt(s.length()); //2 //把index作为下标,来取得字符串的中某一个字符 char c = s.charAt(index); //指定文字的颜色----深色段 g.setColor(getColor(0,120)); //创建一个字体 Font f = new Font("隶书",Font.ITALIC|Font.BOLD,60+rd.nextInt(60)); //把字体关联到画笔 g.setFont(f); code+=c; //把生成的字符连接成一个字符串 //把文字输出到图片上 g.drawString(String.valueOf(c), 100+i*80+rd.nextInt(100),200+rd.nextInt(150)); } request.getSession().setAttribute("code",code);//把生成的验证码信息,存储到session中,登录的时候,用来作判断 ------------------

文件下载

资源详情

[{"title":"( 18 个子文件 443KB ) 采用ajax实现的图片验证码","children":[{"title":"基于ajax的图形验证码","children":[{"title":"ajax_28_4_1","children":[{"title":".project <span style='color:#111;'> 1.38KB </span>","children":null,"spread":false},{"title":".settings","children":[{"title":".jsdtscope <span style='color:#111;'> 500B </span>","children":null,"spread":false},{"title":"org.eclipse.core.resources.prefs <span style='color:#111;'> 100B </span>","children":null,"spread":false},{"title":"org.eclipse.jdt.core.prefs <span style='color:#111;'> 330B </span>","children":null,"spread":false},{"title":"org.eclipse.wst.jsdt.ui.superType.container <span style='color:#111;'> 49B </span>","children":null,"spread":false},{"title":"org.eclipse.wst.jsdt.ui.superType.name <span style='color:#111;'> 6B </span>","children":null,"spread":false}],"spread":true},{"title":".mymetadata <span style='color:#111;'> 306B </span>","children":null,"spread":false},{"title":"WebRoot","children":[{"title":"WEB-INF","children":[{"title":"dwr.xml <span style='color:#111;'> 287B </span>","children":null,"spread":false},{"title":"web.xml <span style='color:#111;'> 977B </span>","children":null,"spread":false},{"title":"lib","children":[{"title":"dwr.jar <span style='color:#111;'> 477.97KB </span>","children":null,"spread":false}],"spread":true},{"title":"classes","children":[{"title":"org","children":[{"title":"java","children":[{"title":"web","children":[{"title":"PicServlet.class <span style='color:#111;'> 3.17KB </span>","children":null,"spread":false}],"spread":true},{"title":"service","children":[{"title":"AjaxService.class <span style='color:#111;'> 912B </span>","children":null,"spread":false}],"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"index.jsp <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"META-INF","children":[{"title":"MANIFEST.MF <span style='color:#111;'> 36B </span>","children":null,"spread":false}],"spread":true},{"title":"js","children":[{"title":"script.js <span style='color:#111;'> 503B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":".myeclipse","children":null,"spread":false},{"title":"src","children":[{"title":"org","children":[{"title":"java","children":[{"title":"web","children":[{"title":"PicServlet.java <span style='color:#111;'> 3.20KB </span>","children":null,"spread":false}],"spread":true},{"title":"service","children":[{"title":"AjaxService.java <span style='color:#111;'> 591B </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":".classpath <span style='color:#111;'> 428B </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明