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. centos 7 安装 Oracle 12c

    #!/bin/bash #!/usr/bin/expect -f #/etc/sysctl.conf --bash-srcipts-- echo 'net.ipv6.conf.all.disable_ ...

  2. MVC中 global.asax

    MVC框架下 global.asax 页面的事件 这些事件被触发的 顺序是: Application_BeginRequest Application_AuthenticateRequest Appl ...

  3. ZenCart通过Contact Us接收垃圾邮件的过滤方案

    最近收到一些通过Contact Us进行垃圾外链群发的邮件,虽然可以通过在Contact Us增加验证码来解决,但不利于客户体验.所以我们可以通过简单的关键词过滤来实现,一般垃圾外链都含有“[url= ...

  4. hdu2159 二维02bag

    设f[i][j]为杀第j只怪时耐久度为i的最大经验值 完全背包类型:有N种物品和一个容量为V 的背包,每种物品都有无限件可用.放入第i种物品的耗费的空间是Ci,得到的价值是Wi. 求解:将哪些物品装入 ...

  5. 高性能mysql 第11章 可扩展的mysql

    可扩展性的定义:当增加资源以获得执行更多的工作系统能获得划算的同等提升. 向上扩展(垂直扩展):提升服务器的硬件性能. 向外扩展(水平扩展):一般都是复制,拆分,数据分片(sharding). 复制: ...

  6. volatile关键字解决线程间内存共享变量同步的问题,让变量可以立即同步。

  7. shell脚本if语句后面的中括号[]与java的if后面的小括号不同(),实际上[左中括号相当于test命令

    四.shell 中的条件判断命令 test 和 [   test 命令可以处理 shell 脚本中的各类工作.它产生的不是一般的输出,而是可使用的退出状态.test 命令通过接受各种不同的参数,来控制 ...

  8. python 异常处理(五)

    异常处理&异常基类 1.处理异常 try.....except 语法: 1) try: 放可能会出现问题的代码 except: 处理错误的方式 例如: try: print(ab)  #无错执 ...

  9. [采坑] VS2015 warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失

    问题: Visual Studio 2015出现warning C4819: 该文件包含不能在当前代码页(936)中表示的字符.请将该文件保存为 Unicode 格式以防止数据丢失. 解决方案: 1. ...

  10. nopCommerce4.10学习笔记——入门

    1.下载 千万不要去GitHub上下载,千万不要去GitHub上下载,千万不要去GitHub上下载!!!,重要的事情说3遍,说多了都是泪,你懂的 下载网址:https://www.nopcommerc ...