web开发(十) struts2之图片验证码
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之图片验证码的更多相关文章
- Django中web开发用md5加密图片名并存储静态文件夹
一般在开发中,有的网站存在大量图片,首先图片的名称是不能重复的, 但是除了数据库可用的id以外我们可以用time模块中time.time()获取的时间来进行md5加密操作, 因为time模块所产生的时 ...
- struts2实现图片验证码
生成图片验证码的主要工具类方法为: package com.yeting.fc.util; import java.awt.Color; import java.awt.Font; import ja ...
- Java Web 开发利用Struts2+Spring+mybatis写一个用户登录界面以及简单的数据交互
框架的东西太复杂也难以讲通,直接上代码: 一.首先得配置环境 和导入必要的jar包 有一些重要的如下: Filter文件夹下的SafetyFilter.java model文件夹下的 Global ...
- Web 开发人员和设计师必读文章推荐【系列三十】
<Web 前端开发精华文章推荐>2014年第9期(总第30期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...
- 使用.Net Core 2.1开发Captcha图片验证码服务
更新后续篇:Captcha服务(后续1) 使用.Net Core 2.1开发Captcha验证码服务 开发工具:Visual Studio 2017 15.7.3 开发平台:64位 Windows 1 ...
- 转: 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
from: https://github.com/RubyLouvre/agate/issues/8 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以 ...
- Web 开发人员和设计师必读文章推荐【系列二十九】
<Web 前端开发精华文章推荐>2014年第8期(总第29期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
- Web 开发最有用的50款 jQuery 插件集锦——《图片特效篇》
<Web 开发最有用的50款 jQuery 插件集锦>系列文章向大家分享最具创新的50款 jQuery 插件,这些插件分成以下类别:网页布局插件,导航插件,表格插件,滑块和转盘插件,图表插 ...
随机推荐
- 【TJOI 2019】唱、跳、rap和篮球
题意 有 $a$ 个 $0$,$b$ 个 $1$,$c$ 个 $2$,$d$ 个 $3$,求有多少种长度为 $n$ 且不包含 $0123$ 这个子串的字符串个数. $n\le 1000,\space ...
- 微擎后台进行GET提交
微擎form表单进行GET提交时,要传递 name 分别为 c , a , m , do 的值 例如: <form action="{php echo $this->create ...
- 关于Mongodb的其他知识
Mongodb支持的数据类型 数据类型 描述 String 字符串.存储数据常用的数据类型.在 MongoDB 中,UTF-8 编码的字符串才是合法的. Integer 整型数值.用于存储数值.根据你 ...
- 2019全国卷(III)理科23题的另类解法
已知 $x,y,z\in\textbf{R}$且$x+y+z=1$ (1)求$(x-1)^2+(y+1)^2+(z+1)^2$的最小值: (2)若$(x-2)^2+(y-1)^2+(z-a)^2\ge ...
- ThinkPHP 模型方法 setInc() 和 setDec()
TP 内置了对统计数据(数字字段)的更新方法: setInc():将数字字段值增加 setDec():将数字字段值减少 $User::where('id=5')->setInc('score', ...
- php的$_get,$_post用法
$_GET 可以被收藏, 可以被缓存, 可以保存在历史记录中, 可以提交请求但是很不安全, 长度有限制在2000个字符,其实get请求就是一个url;$_GET['user_name'] $_POST ...
- JSP中的四种作用域?
page.request.session和application,具体如下: ①page 代表与一个页面相关的对象和属性. ②request 代表与Web客户机发出的一个请求相关的对象和属性.一个请求 ...
- 【C#-算法】根据生日自动计算年龄_DataTime 的 DateDiff 方法
dateTimePicker1.Value出生日期控件的值 long BirthDay = DateAndTime.DateDiff(DateInterval.Year, dateTimePicker ...
- 【leetcode】1012. Numbers With Repeated Digits
题目如下: Given a positive integer N, return the number of positive integers less than or equal to N tha ...
- Cookie相关工具方法
/** * InputStream转化为byte[]数组 * @param input * @return * @throws IOException */ public static byte[] ...