Struts2中的图片验证码
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中的图片验证码的更多相关文章
- 在mvc中实现图片验证码的刷新
首先,在项目模型(Model)层中建立一个生成图片验证码的类ValidationCodeHelper,代码如下: public class ValidationCodeHelper { //用户存取验 ...
- django项目登录中使用图片验证码
应用下创建untils文件夹放置封装图片验证码的函数 创建validCode.py文件定义验证码规则 import random def get_random_color(): return (ran ...
- Struts2中实现随机验证码
一.创建RandomNum类 1: import java.awt.Color; 2: import java.awt.Font; 3: import java.awt.Graphics; 4: im ...
- struts向网页输出图片验证码
前言:今天做个功能需要展示图片到页面,并不是下载,在网上搜了老半天,大部分都是下载,有的话也是只能在IE下进行输出,其它浏览器就都是下载了. Action代码: public String proce ...
- ASP.NET图片验证码
1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...
- ASP.NET图片验证码学习!
1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...
- Django实战(一)-----用户登录与注册系统5(图片验证码)
为了防止机器人频繁登录网站或者破坏分子恶意登录,很多用户登录和注册系统都提供了图形验证码功能. 验证码(CAPTCHA)是一种区分用户是计算机还是人的公共全自动程序. 可以防止恶意破解密码.刷票.论坛 ...
- Django商城项目笔记No.4用户部分-注册接口-图片验证码
Django商城项目笔记No.4用户部分-注册接口-图片验证码 1.首先分析注册业务接口 1.1.分析可得,至少这么几个接口 图片验证码 短信验证码 用户名是否存在 手机号是否存在 整体注册接口 图片 ...
- linux下tomcat6无法显示图片验证码 少了图形插件
linux下tomcat6无法显示图片验证码(windows下显示正常) 原创 2015年10月20日 10:31:47 3526 linux下tomcat6无法显示图片验证码(windows下显示正 ...
随机推荐
- February 8th, 2018 Week 6th Thursday
When you fall in love, friends, let yourself fall. 当你坠入爱河,我的朋友,你就放手去爱吧. To love someone is like movi ...
- Orcale的NVL、NVL2函数和SQL Server的ISNULL函数
Orcal 的 nvl函数 NVL(Expr1,Expr2)如果Expr1为NULL,返回Expr2的值,否则返回Expr1的值,Expr1,Expr2都为NULL则返回NULL NVL2(Expr1 ...
- [ ArcGIS for Server 10.1 系列 ] - 重新创建Site
一般当ArcGIS Server Site发生错误.ArcGIS Server无法启动或者ArcGIS Server某服务没有实例,就可能需要重新的创建Site.有时可以通过重新创建Site,就发现其 ...
- 【洛谷】【模拟+栈】P4711 「化学」相对分子质量
[题目传送门:] [戳] (https://www.luogu.org/problemnew/show/P4711) [算法分析:] 关于一个分子拆分后的产物,一共有三种情况: 原子 原子团 水合物 ...
- Qt 编程指南 4 按钮
1按钮类的控件 逐个解释一下各个用途:(1)按压按钮 QPushButton最基本的按钮,点击该按钮通常是通知程序进行一个操作,比如弹个窗.下一步.保存.退出等等,这是经常用到的,操作系统里的对话框里 ...
- ORACLE 11gR2 RAC添加删除(正常及强制)节点操作步骤(删除篇)
ORACLE 11gR2 RAC添加删除(正常及强制)节点操作步骤(删除篇) 本文主要转载 [ http://www.cnxdug.org/?p=2511 ] 有部分细节自己实验添加,再此谢谢前辈. ...
- 树莓派3b安装Apache2+PHP+MySQL+phpyadmin
树莓派型号:3B+ 系统环境:2017-04-10-raspbian-jessie 先更新一下源 BASIC sudo apt-get update 安装Apache2 BASIC sudo apt- ...
- 如何在Spring MVC Test中避免”Circular view path” 异常(转)
文章转自http://www.cnblogs.com/chry/p/6240965.html 1. 问题的现象 比如在webConfig中定义了一个viewResolver public class ...
- Java并发(五)线程池使用番外-分析RejectedExecutionException异常
目录 一.入门示例 二.异常场景1 三.异常场景2 四.解决方法 之前在使用线程池的时候,出现了 java.util.concurrent.RejectedExecutionException ,原因 ...
- spring-cloud-starter-feign 等jar无法reimport的解决方案
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...