1.Struts中建一个action

<action name="Code" class="LoginAction" method="code">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">2048</param>
</result></action>

2.写Code.action

首先定义inputStream,并get,set

private ByteArrayInputStream inputStream; 
 public ByteArrayInputStream getInputStream() {
return inputStream;
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}

然后Code.action代码如下

 public String code() throws Exception {

    	 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(); ImageIO.write(image, "jpeg", outputStream); ByteArrayInputStream input = new ByteArrayInputStream(outputStream.toByteArray()); this.setInputStream(input); HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("checkCode", rands); input.close(); outputStream.close();
return SUCCESS;
}

3.jsp中调用

 <form id="form" action="Login.action" method="post">
<table border=0 cellpadding="4">
<tr><td><input type="text" name="username" placeholder="学号/工号/用户名" size="22" style="background-image: url(img/user.JPG);"><br></td></tr>
<tr><td><input type="password" name="password" placeholder="密码" size="22" style="background-image: url(img/password.JPG);"><br></td></tr>
<tr><td>
<input type="text" placeholder="验证码" name="checkCode" size="10"/>
<img alt="" src="Code.action" onclick="this.src='Code.action?'+ Math.random();" title="点击图片刷新验证码">
</td> </tr>
<tr><td><input type="submit" class="btnCheck" value="登录" onclick="validate()" style="width:100%;"></button></td>
</tr>
</table>
</form>

4.在另一个action中验证

HttpSession session = ServletActionContext.getRequest().getSession();
String checkCode2 = (String)session.getAttribute("checkCode");
System.out.println(checkCode+"aaa"+checkCode2);
if(!checkCode.equals(checkCode2))
{
return "chekCodeerror";
}

5.结果截图

  

Struts2中的图片验证码的更多相关文章

  1. 在mvc中实现图片验证码的刷新

    首先,在项目模型(Model)层中建立一个生成图片验证码的类ValidationCodeHelper,代码如下: public class ValidationCodeHelper { //用户存取验 ...

  2. django项目登录中使用图片验证码

    应用下创建untils文件夹放置封装图片验证码的函数 创建validCode.py文件定义验证码规则 import random def get_random_color(): return (ran ...

  3. Struts2中实现随机验证码

    一.创建RandomNum类 1: import java.awt.Color; 2: import java.awt.Font; 3: import java.awt.Graphics; 4: im ...

  4. struts向网页输出图片验证码

    前言:今天做个功能需要展示图片到页面,并不是下载,在网上搜了老半天,大部分都是下载,有的话也是只能在IE下进行输出,其它浏览器就都是下载了. Action代码: public String proce ...

  5. ASP.NET图片验证码

    1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...

  6. ASP.NET图片验证码学习!

    1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...

  7. Django实战(一)-----用户登录与注册系统5(图片验证码)

    为了防止机器人频繁登录网站或者破坏分子恶意登录,很多用户登录和注册系统都提供了图形验证码功能. 验证码(CAPTCHA)是一种区分用户是计算机还是人的公共全自动程序. 可以防止恶意破解密码.刷票.论坛 ...

  8. Django商城项目笔记No.4用户部分-注册接口-图片验证码

    Django商城项目笔记No.4用户部分-注册接口-图片验证码 1.首先分析注册业务接口 1.1.分析可得,至少这么几个接口 图片验证码 短信验证码 用户名是否存在 手机号是否存在 整体注册接口 图片 ...

  9. linux下tomcat6无法显示图片验证码 少了图形插件

    linux下tomcat6无法显示图片验证码(windows下显示正常) 原创 2015年10月20日 10:31:47 3526 linux下tomcat6无法显示图片验证码(windows下显示正 ...

随机推荐

  1. ABAP性能和优化

    哪些工具可以用于性能优化? ST05-性能追踪.包含SQL追踪加RFC,队列和缓存追踪.SQL追踪主要用于测量程序中select语句的性能. SE30-运行时分析.用于测量应用的性能. SAT是过时的 ...

  2. Activiti工作流的定义部署和执行

        工作流引擎 个人觉得直接理解工作流引擎概念有点难度,我们可以先通过了解工作流引擎的职责再反过来理解工作流引擎,工作流引擎一般都做两件事情: 1.定义流程,也就是给我们提供某种规范来定义规则,以 ...

  3. 西安80投影坐标系转WGS84地理坐标系如何求七参数

    需求:西安80投影坐标系(平面坐标)转为WGS84地理坐标系(球面坐标) 这其中涉及的问题主要有以下两点: 1.一个是投影坐标系,一个是地理坐标系,而七参数指的是两个地理坐标系之间的转换,因此需要把投 ...

  4. vue_02 开发过程中的问题记载

    1.package.json 运行 npm start 执行的是npm run  dev 实际上执行的是“dev” : node build/dev-server.js这一条 跑的是build目录下d ...

  5. 不可不知 DDoS的攻击原理与防御方法

    DoS攻击.DDoS攻击和DRDoS攻击相信大家已经早有耳闻了吧!DoS是Denial of Service的简写就是拒绝服务,而DDoS就是Distributed Denial of Service ...

  6. data函数参数

    date(format,timestamp); 参数 描述 format 必需.规定输出日期字符串的格式.可使用下列字符: d - 一个月中的第几天(从 01 到 31) D - 星期几的文本表示(用 ...

  7. ROS教程0 环境配置

    1安装环境配置(桌面进入命令行) echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc source ~/.bas ...

  8. PCB (3)创建新工程PCB

    点击完成 拖进我们创建的工程中

  9. Qt5.7 + VS2015 环境搭建

    http://blog.csdn.net/liang19890820/article/details/53931813#安装插件 配置 Qt 5.7 选择:Qt VS Tools -> Qt O ...

  10. Cookie、sessionStorage与localStorage的区别

    (1) sessionStorage 保存数据的方法: SessionStor.setItem(“key”,”value”) 或者写成 sessionStorage.key=”value” 读取数据的 ...