1、配置前端页面

 <!-- 验证码-->
<div class="form-group " style="padding-left: 9%;" id="show">
<span class="glyphicon glyphicon-picture pull-left input-group" style="padding-top: 8px;"></span>
<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%">
<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%;"/>
<small class="control-label pull-left" style="margin-top: 10px;margin-left: 5px;">(点击图片刷新)</small>
<label class="control-label pull-left" id="msg_code" style="color: red;margin-top: 3px;margin-left: 5px;"></label>
</div>

login.jsp

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

2、配置Action

 <action name="Code" class="Action.LoginOrRegisterAction" method="getCheckCodePic">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="input2">inputStream</param>
<param name="bufferSize">2048</param>
</result>
</action>

struts.xml

3、Action实例

  //获取验码图片
public String getCheckCodePic() {
int WIDTH = 60;
int HEIGHT = 20;
HttpServletResponse response = ServletActionContext.getResponse(); // 设置浏览器不要缓存此图片
response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); String str = "0123456789qwertyuiopasdfghjklzxcvbnm"; char[] rand = new char[4]; Random random = new Random(); for (int i = 0; i < 4; i++)
{
rand[i] = str.charAt(random.nextInt(36));
} String rands =new String(rand); BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // 产生图像
// 画背景
g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, WIDTH, HEIGHT); // 随机产生 120 个干扰点 for (int i = 0; i < 120; i++)
{
int x = (int) (Math.random() * WIDTH); int y = (int) (Math.random() * HEIGHT); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); g.setColor(new Color(red, green, blue)); g.drawOval(x, y, 1, 0);
} g.setColor(Color.BLACK); g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18)); // 在不同的高度上输出验证码的每个字符 g.drawString("" + rands.charAt(0), 1, 17); g.drawString("" + rands.charAt(1), 16, 15); g.drawString("" + rands.charAt(2), 31, 18); g.drawString("" + rands.charAt(3), 46, 16); System.out.println(rands); // 结束图像 的绘制 过程, 完成图像
g.dispose(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try {
ImageIO.write(image, "jpeg", outputStream); ByteArrayInputStream input = new ByteArrayInputStream(outputStream.toByteArray()); this.setInputStream(input);
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("checkCode", rands);
input.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}

LoginOrRegisterAction

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

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

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. 青风nrf52832跑zephyr——点亮LED

    zephyr版本:1.10 硬件:采用青风nrf52832开发板 开发环境:虚拟机Ubuntu16.04编译+Windows7 64bit烧录   使用的是 zephyr-zephyr-v1.10.0 ...

  2. composer入门教程

    初始化项目 使用composer初始化工作目录,在项目的根目录命令行输入 composer init 安装项目 在composer.json文件所在目录命令行下执行如下命令 php composer. ...

  3. uestc summer training #4 牛客第一场

    A dp[i][j][k]可以n3地做 但是正解是找把问题转化为一个两点不相交路径 最终答案为C(n+m, n)2-C(n+m, m-1)C(n+m,n-1) B 把题目的矩阵看成无向图的邻接矩阵 这 ...

  4. [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理]

    [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理] 实用知识 错误处理 我们今天来讲讲Rust中的错误处理. 很多语言都有自己的错误处理方式,比如,java是异常处理机制. ...

  5. SpringBoot项目多模块打包与部署【pom文件问题】

    [bean的pom] [user的pom] 特别注意,user模块因为有返回jsp页面和web相关,所以需要加入web依赖. chapter23 com.yuqiyu 1.0.0 4.0.0 com. ...

  6. 更新protobuf

    sudo pip install --upgrade protobuf sudo apt-get install libprotobuf-dev protobuf-compiler这个不知道是什么问题 ...

  7. vue iOS上传图片file 出错

    前言 用vue 移动端上传图片在低版本的 ios 手机上 图片转换base64 在转换file 文件类型 会报错 并且报错 “Script Error ” 查阅了github 和一些文档发现 可以吧 ...

  8. JAVA记录 Spring 两大特性

    1.IOC控制反转 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  9. 2、DockPanel

    DockPanel——停靠面板,内部控件或容器可以放置在上.下.左(默认).右.类似于Java AWT布局中的BorderLayout. 但与BorderLayout不同的是,每一个区域可以同时放置多 ...

  10. javascript中的原型和原型链(一)

    原型和原型链是 JS 中不可避免需要碰到的知识点,本文使用图片思维导图的形式缕一缕原型.原型链.实例.构造函数等等概念之间的关系. Constructor 构造函数 首先我们先写一个构造函数 Pers ...