1、配置前端页面

  1. <!-- 验证码-->
  2. <div class="form-group " style="padding-left: 9%;" id="show">
  3. <span class="glyphicon glyphicon-picture pull-left input-group" style="padding-top: 8px;"></span>
  4. <input type="text" class="form-control col-md-7 pull-left" maxlength="4" id="user.user_code" name="user.user_code" placeholder="验证码" style="width:100px;margin-left: 2%">
  5. <img alt="" src="Code.action" src="video/Crocodile.jpg" onclick="this.src='Code.action?'+ Math.random();" class="pull-left" style="width:65px;height: 35px;margin-left: 2%;"/>
  6. <small class="control-label pull-left" style="margin-top: 10px;margin-left: 5px;">(点击图片刷新)</small>
  7. <label class="control-label pull-left" id="msg_code" style="color: red;margin-top: 3px;margin-left: 5px;"></label>
  8. </div>

login.jsp

其中  src="Code.action"  onclick="this.src='Code.action?'+ Math.random();",分别为action的name和每次点击图片都自动刷新一次

2、配置Action

  1. <action name="Code" class="Action.LoginOrRegisterAction" method="getCheckCodePic">
  2. <result name="success" type="stream">
  3. <param name="contentType">image/jpeg</param>
  4. <param name="input2">inputStream</param>
  5. <param name="bufferSize">2048</param>
  6. </result>
  7. </action>

struts.xml

3、Action实例

  1. //获取验码图片
  2. public String getCheckCodePic() {
  3. int WIDTH = 60;
  4. int HEIGHT = 20;
  5. HttpServletResponse response = ServletActionContext.getResponse();
  6.  
  7. // 设置浏览器不要缓存此图片
  8. response.setHeader("Pragma", "no-cache");
  9.  
  10. response.setHeader("Cache-Control", "no-cache");
  11.  
  12. response.setDateHeader("Expires", 0);
  13.  
  14. String str = "0123456789qwertyuiopasdfghjklzxcvbnm";
  15.  
  16. char[] rand = new char[4];
  17.  
  18. Random random = new Random();
  19.  
  20. for (int i = 0; i < 4; i++)
  21. {
  22. rand[i] = str.charAt(random.nextInt(36));
  23. }
  24.  
  25. String rands =new String(rand);
  26.  
  27. BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
  28.  
  29. Graphics g = image.getGraphics();
  30.  
  31. // 产生图像
  32. // 画背景
  33. g.setColor(new Color(0xDCDCDC));
  34.  
  35. g.fillRect(0, 0, WIDTH, HEIGHT);
  36.  
  37. // 随机产生 120 个干扰点
  38.  
  39. for (int i = 0; i < 120; i++)
  40. {
  41. int x = (int) (Math.random() * WIDTH);
  42.  
  43. int y = (int) (Math.random() * HEIGHT);
  44.  
  45. int red = (int) (Math.random() * 255);
  46.  
  47. int green = (int) (Math.random() * 255);
  48.  
  49. int blue = (int) (Math.random() * 255);
  50.  
  51. g.setColor(new Color(red, green, blue));
  52.  
  53. g.drawOval(x, y, 1, 0);
  54. }
  55.  
  56. g.setColor(Color.BLACK);
  57.  
  58. g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
  59.  
  60. // 在不同的高度上输出验证码的每个字符
  61.  
  62. g.drawString("" + rands.charAt(0), 1, 17);
  63.  
  64. g.drawString("" + rands.charAt(1), 16, 15);
  65.  
  66. g.drawString("" + rands.charAt(2), 31, 18);
  67.  
  68. g.drawString("" + rands.charAt(3), 46, 16);
  69.  
  70. System.out.println(rands);
  71.  
  72. // 结束图像 的绘制 过程, 完成图像
  73. g.dispose();
  74.  
  75. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  76.  
  77. try {
  78. ImageIO.write(image, "jpeg", outputStream);
  79.  
  80. ByteArrayInputStream input = new ByteArrayInputStream(outputStream.toByteArray());
  81.  
  82. this.setInputStream(input);
  83. Map<String, Object> session = ActionContext.getContext().getSession();
  84. session.put("checkCode", rands);
  85. input.close();
  86. outputStream.close();
  87. } catch (IOException e) {
  88. // TODO Auto-generated catch block
  89. e.printStackTrace();
  90. }
  91. return SUCCESS;
  92. }

LoginOrRegisterAction

4、另一个Action(进行表单验证,通过获取之前保存Session的key)

  1. //验证用户码是否正确
  2. Map<String, Object> session1=ActionContext.getContext().getSession();
  3. if(user.getUser_code()!=null && "".equals(user.getUser_code())==false) {
  4. String code=(String) session1.get("checkCode");
  5. if(code.equals(user.getUser_code())==false) {
  6. //验证码错误,返回状态值
  7. map2.put("LoginStatus", "303");
  8. System.out.println("验证码错误");
  9. String ReturnData=JSONObject.toJSONString(map2); //把Map集合封装成Json形式的字符串
  10. inputStream=new ByteArrayInputStream(ReturnData.getBytes("UTF-8"));
  11. return SUCCESS;
  12. }
  13.  
  14. }

LoginAction

web开发(十) struts2之图片验证码的更多相关文章

  1. Django中web开发用md5加密图片名并存储静态文件夹

    一般在开发中,有的网站存在大量图片,首先图片的名称是不能重复的, 但是除了数据库可用的id以外我们可以用time模块中time.time()获取的时间来进行md5加密操作, 因为time模块所产生的时 ...

  2. struts2实现图片验证码

    生成图片验证码的主要工具类方法为: package com.yeting.fc.util; import java.awt.Color; import java.awt.Font; import ja ...

  3. Java Web 开发利用Struts2+Spring+mybatis写一个用户登录界面以及简单的数据交互

    框架的东西太复杂也难以讲通,直接上代码: 一.首先得配置环境 和导入必要的jar包 有一些重要的如下: Filter文件夹下的SafetyFilter.java   model文件夹下的 Global ...

  4. Web 开发人员和设计师必读文章推荐【系列三十】

    <Web 前端开发精华文章推荐>2014年第9期(总第30期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  5. 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...

  6. 使用.Net Core 2.1开发Captcha图片验证码服务

    更新后续篇:Captcha服务(后续1) 使用.Net Core 2.1开发Captcha验证码服务 开发工具:Visual Studio 2017 15.7.3 开发平台:64位 Windows 1 ...

  7. 转: 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    from: https://github.com/RubyLouvre/agate/issues/8 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以 ...

  8. Web 开发人员和设计师必读文章推荐【系列二十九】

    <Web 前端开发精华文章推荐>2014年第8期(总第29期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  9. Web 开发最有用的50款 jQuery 插件集锦——《图片特效篇》

    <Web 开发最有用的50款 jQuery 插件集锦>系列文章向大家分享最具创新的50款 jQuery 插件,这些插件分成以下类别:网页布局插件,导航插件,表格插件,滑块和转盘插件,图表插 ...

随机推荐

  1. golang连接activemq,发送接收数据

    介绍 使用golang连接activemq发送数据的话,需要使用一个叫做stomp的包,直接go get github.com/go-stomp/stomp即可 代码 生产者 package main ...

  2. ceph问题汇总

    1. [ceph_deploy][ERROR ]RuntimeError: Failed to execute command: yum -y install epel-release 解决方案 进入 ...

  3. Lua语言基本语法~运算符

    Lua 变量 变量在使用前,必须在代码中进行声明,即创建该变量. 编译程序执行代码之前编译器需要知道如何给语句变量开辟存储区,用于存储变量的值. Lua 变量有三种类型:全局变量.局部变量.表中的域. ...

  4. 动画学习之Music图形绘制

    今天来实现一个类似于网易云音乐类似的动态效果,在用网易云音乐听歌时会有一个类似这样的效果,如下: 而咱们这次要实现的效果如下: music图形的绘制: 在实现动画之前先来将静态的图形绘制出来, 如下: ...

  5. centos7安装kong和kong-dashboard

    1.安装Kong yum install -y https://kong.bintray.com/kong-community-edition-rpm/centos/7/kong-community- ...

  6. Adboost几个要点分析

    1.本质就是前向步进算法和加法模型,每一步计算分类器权重alpha和基分类器. 2.总体降低指数误差函数,转化为每一步降低分类误差率. 因为右边可以看作常数,所以相当于对这个进行优化 但是这一步可能做 ...

  7. http的get与post

    1.http请求 http有两种报文,请求报文 (发送请求,可能包含数据)和响应报文(服务器响应请求获取数据).一个http请求报文由请求行,请求头部,空行和请求正文(数据)四个部分组成. HTTP请 ...

  8. HTML的状态码

    HTML状态码的相关知识 ㈠:含义 HTTP状态码(英语:HTTP Status Code)是用以表示网页服务器超文本传输协议响应状态的3位数字代码. 也就是当浏览者访问一个网页时,浏览者的浏览器会向 ...

  9. Word:图片压缩

    造冰箱的大熊猫,本文适用于Microsoft Word 2007@cnblogs 2018/12/1 图片插入Word文档后,可以通过“裁剪”功能只显示图片的部分区域.虽然文档中显示的图片区域变小了, ...

  10. (转载)rabbitmq与springboot的安装与集成

    原文地址:https://segmentfault.com/a/1190000016991529 一.前言 RabbitMQ是一个开源的消息代理软件(面向消息的中间件),它的核心作用就是创建消息队列, ...