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 插件,这些插件分成以下类别:网页布局插件,导航插件,表格插件,滑块和转盘插件,图表插 ...
随机推荐
- 4.(基础)tornado应用安全与认证
这一节我们介绍应用安全与认证,其实中间省略了一个数据库.对于tornado来说,读取数据库的数据,性能的瓶颈还是在数据库上面.关于数据库,我在<>中介绍了sqlalchemy,这是一个工业 ...
- 1.SpringBoot整合Mybatis(CRUD的实现)
准备工具:IDEA jdk1.8 Navicat for MySQL Postman 一.新建Project 选择依赖:mybatis Web Mysql JDBC 项目结构 pom依赖: & ...
- Share:《THE ULTIMATE XSS PROTECTION CHEATSHEET FOR DEVELOPERS》
Ajin Abraham(OWASP Xenotix XSS Exploit Framework的作者哦!)编写的<THE ULTIMATE XSS PROTECTION CHEATSHEET ...
- .htaccess 一段神奇的跳转代码
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_REFERER} ^.*(google|ask|yahoo|you ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理]
[易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理] 实用知识 错误处理 我们今天来讲讲Rust中的错误处理. 很多语言都有自己的错误处理方式,比如,java是异常处理机制. ...
- 如何查看fullGC 次数
如何查看fullGC 次数 如何较少fullGC 如何保证几周才发生一次fullGC
- 加密web.config数据库连接
加密cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319aspnet_regiis.exe -pef "connectionStrings&q ...
- LightOJ-1079-Just another Robbery(概率, 背包)
链接: https://vjudge.net/problem/LightOJ-1079#author=feng990608 题意: As Harry Potter series is over, Ha ...
- ToolStrip 选中某一项打勾
(sender as ToolStripMenuItem).Checked = !(sender as ToolStripMenuItem).Checked;
- vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)
一.安装echarts: cnpm i echarts -D 二.在vue-cli的main.js文件中引用echarts: import charts from 'echarts' Vue.prot ...