基于SSH2框架下的 用户注册 验证码功能
来源:互联网 发布:魅族手淘宝网的价格 编辑:程序博客网 时间:2024/05/01 15:10
jsp页面:
<tr> <th width="320" >验证码:</th> <td width="741" > <s:textfield key="registerInfoBean.checkNumber" cssClass="inputtext" data-rule-required="true" /> <img id="chkNumber" src="registerChkNumber.action" /> </td> </tr>
js:
//验证码 $("#chkNumber").click(function(){ var timenow = new Date().getTime(); $(this).attr("src", "registerChkNumber.action?d="+timenow); });struts.xml文件配置;
<action name="registerChkNumber" class="registerAction" method="getChkNumber"> <result type="stream"> <param name="contentType">image/jpeg</param> <param name="inputtName">inputStream</param> </result> </action>
Action处理:
//输出流 private ByteArrayInputStream inputStream; /** * 验证码获取 * */ public String getChkNumber(){ this.setInputStream(this.registerService.getChkNumber()); return SUCCESS; }
Service实现类方法:
/** * 获取验证码 */ public ByteArrayInputStream getChkNumber(){ int width = 85, height = 25; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); Random random = new Random(); g.setColor(getRandColor(200, 255)); g.fillRect(0, 0, width, height); g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); g.setColor(getRandColor(160, 200)); for(int i = 0; i < 155; i++) { int x1 = random.nextInt(width); int y1 = random.nextInt(height); int x2 = random.nextInt(12); int y2 = random.nextInt(12); g.drawLine(x1, y1, x2, y2); } String sRand = ""; for(int i = 0; i < 6; i++) { String rand = getRandomChar(); sRand += rand; g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(rand, 13*i+6, 16); } //存进session,用于验证 ActionContext.getContext().getSession().put("randomImg", sRand.toLowerCase()); g.dispose(); ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageOutputStream imageOut; try { imageOut = ImageIO.createImageOutputStream(output); ImageIO.write(image, "JPEG", imageOut); imageOut.close(); } catch (IOException e) { e.printStackTrace(); } ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); return input; } //颜色 public 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); } //产生随机数 public String getRandomChar(){ int rand = (int)Math.round(Math.random() * 2); long itmp = 0; char ctmp = '\u0000'; switch (rand) { case 1: itmp = Math.round(Math.random() * 25 + 65); ctmp = (char)itmp; return String.valueOf(ctmp); case 2: itmp = Math.round(Math.random() * 25 + 97); ctmp = (char)itmp; return String.valueOf(ctmp); default : itmp = Math.round(Math.random() * 9); } return String.valueOf(itmp); }
验证提交验证码是否正确
//判断验证码是否正确 if(registerInfo.getCheckNumber().toLowerCase().equals( ActionContext.getContext().getSession().get("randomImg"))){ // 正确后的业务处理代码}
0 0
- 基于SSH2框架下的 用户注册 验证码功能
- Django框架下用户注册邮箱验证功能的实现
- SSH2框架实现注册发短信验证码实例
- 基于SSH2框架Struts2拦截器的登录验证实现
- 基于SSH2框架Struts2拦截器的登录验证实现
- 基于ssh2架构的struts2验证框架错误解决
- 基于SSH2框架Struts2拦截器的登录验证实现
- 基于SSH2框架Struts2拦截器的登录验证实现
- 基于SSH2框架Struts2拦截器的登录验证实现
- SSM框架下的注册验证
- 基于SSH2框架Struts2拦截器的登录验证实现 (转)
- 基于SSH2框架Struts2拦截器的登录验证实现 来自菠萝大象
- Android实战简易教程-第二十三枪(基于Baas的用户注册验证用户名是否重复功能!)
- Android实战简易教程<二十三>(基于Baas的用户注册验证用户名是否重复功能!)
- 基于JSP的AJAX的实现(用户注册验证)
- Struts2验证框架验证用户注册
- struts2验证框架后台验证用户注册
- struts2验证框架后台验证用户注册 .
- A problem displaying [localhost] caused Internet Explorer to refresh the webpage using Compatibility
- OpenCms9学习笔记(二)——OpenCms9下载安装
- 第四周 【项目2-分数类的雏形】
- 自定义实现Json字符串向C#对象的转变
- how to change a symbolic link instead of deleting it?
- 基于SSH2框架下的 用户注册 验证码功能
- 开源的PaaS方案:在OpenStack上部署CloudFoundry (四)使用Micro Bosh部署CloudFoundry
- Gson解析错误
- Roope的Cocos2d-x学习之旅 001:基础概念学习——导演、场景、层、精灵
- C++中main函数执行完毕后还可以执行其他语句么?
- Spring - href访问
- 第四周项目三 对象数组操作长方形类
- 【LeetCode】083.Remove Duplicates from Sorted List
- 开源的PaaS方案:在OpenStack上部署CloudFoundry (五)常见问题