分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

图片验证码jsp

发布时间:2023-09-06 01:42责任编辑:胡小海关键词:jsjsp验证码
<%int width = 60;int height = 32;//create the imageBufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();// set the background colorg.setColor(new Color(0xDCDCDC));g.fillRect(0, 0, width, height);// draw the borderg.setColor(Color.black);g.drawRect(0, 0, width - 1, height - 1);// create a random instance to generate the codesRandom rdm = new Random();String hash1 = Integer.toHexString(rdm.nextInt());System.out.print(hash1);// make some confusionfor (int i = 0; i < 50; i++) {int x = rdm.nextInt(width);int y = rdm.nextInt(height);g.drawOval(x, y, 0, 0);}// generate a random codeString capstr = hash1.substring(0, 4);//将生成的验证码存入sessionsession.setAttribute("validateCode", capstr);g.setColor(new Color(0, 100, 0));g.setFont(new Font("Candara", Font.BOLD, 24));g.drawString(capstr, 8, 24);g.dispose();//输出图片response.setContentType("image/jpeg");out.clear();out = pageContext.pushBody();OutputStream strm = response.getOutputStream();ImageIO.write(image, "jpeg", strm);strm.close();%>

  

图片验证码jsp

原文地址:https://www.cnblogs.com/ldwnana/p/8437958.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved