在JSP验证 验证码,不用Session
来源:互联网 发布:guava 并发编程 编辑:程序博客网 时间:2024/06/07 16:30
在JS生成验证码数字,传入后台,再生成验证码。
1、JSP:
<img id="checkCode" align="middle" title="点击刷新验证码" onclick="getNewCode()" style="cursor: pointer;">
2、JS:var CODE;//全局变量function createCode(){ CODE = ""; var codeLength = 4;//验证码的长度 //所有候选组成验证码的字符,可以用中文 var selectChar = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); for(var i=0;i<codeLength;i++) { var charIndex = Math.floor(Math.random()*60); CODE +=selectChar[charIndex]; } return CODE; } function getNewCode(){ //显示验证码 $("#checkCode").attr("src","/utils/getValidateCodeNewMethod.do?code="+createCode()); }
3、Java:
@RequestMapping("getValidateCodeNewMethod")public void getValidateCodeNewMethod(HttpServletRequest request,HttpServletResponse response,String code)throws Exception{ response.setContentType("image/jpeg"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "No-cache"); response.setDateHeader("Expires", 0L); int width = 80; int height = 30; BufferedImage image = new BufferedImage(width, height, 1); Graphics g = image.getGraphics(); Random random = new Random(); g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); g.setFont(new Font("Fixedsys", Font.BOLD,28)); g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width + 100); int y = random.nextInt(height + 100); int xl = random.nextInt(10); int yl = random.nextInt(12); g.drawOval(x, y, x + xl, y + yl); } String sRand=code; for (int i = 0; i < sRand.length(); i++) { String rand = sRand.substring(i, i+1); g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110))); g.drawString(rand, 14 * i + 5, 25); } g.dispose(); ServletOutputStream imageOut = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(imageOut); encoder.encode(image); } private Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); }
阅读全文
0 0
- 在JSP验证 验证码,不用Session
- 不用session的验证码
- day15-session&jsp&验证码
- jsp中生成验证码,不用session也可以(主要是:思想)
- 技术中的喜闻乐见,不用session存放验证码
- 在servlet中生成验证码在jsp中验证
- 注册时在laravel用session验证手机验证码
- jsp+servlet验证码验证
- 在jsp页中添加验证码
- 如何在jsp中有验证码
- JSP session对象以及图片验证码的实现
- JSP验证码与session不一致处理方法
- 关于jsp页面的验证,在指定的位置出现,不用alert
- jsp中生成的验证码和存在session里面的验证码不一致的处理
- jsp中生成的验证码和存在session里面的验证码不一致的处理
- jsp页面中验证session过期
- 在JSP页面验证邮箱
- 基于C#和JavaScript的网页验证码优化实现方式(不用Session和Cookie)
- 获取透视摄像机的可视区域
- 用递归函数和栈操作逆序栈
- c#面试题
- 模拟微信分配红包
- linux 发现交换文件 ".swp"
- 在JSP验证 验证码,不用Session
- php 生成图片验证码 并表单验证
- openlayer中的style
- HDU1166 敌兵布阵 单点更新+区间求和
- eclipse中error pages中有个红×错误问题
- 二叉树的所有路径
- Dorado7 自定义下拉框(二)
- tinkphp中getField()和field()区别
- 敏捷软件开发宣言