struts2.0中使用图形验证码
来源:互联网 发布:彩虹岛登陆网络传输 编辑:程序博客网 时间:2024/05/17 23:01
产生随机数action:
package com.cetc.struts.action.t00hy;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.cetc.util.BaseAction;
import com.cetc.util.MyDebug;
public class RandomAction extends BaseAction {
private static final long serialVersionUID = -2269414914034390741L;
private Random random;
private BufferedImage image;
private Font font;
private int distance;
public String getCheckCodeImage(String str,int show,OutputStream output) throws IOException{
random = new Random();
image = new BufferedImage(70,25,BufferedImage.TYPE_3BYTE_BGR);
font = new Font("Arial",Font.PLAIN,20);
this.distance = 15;
Graphics d = image.getGraphics();
d.setColor(Color.WHITE);
d.fillRect(0,0,image.getWidth(),image.getHeight());
d.setColor(new Color(random.nextInt(100)+100,random.nextInt(100)+100,random.nextInt(100)+100));
for (int i = 0; i < 10; i++) {
d.drawLine(random.nextInt(image.getWidth()),random.nextInt(image.getHeight()),random.nextInt(image.getWidth()),random.nextInt(image.getHeight()));
}
d.setColor(Color.BLACK);
d.setFont(font);
String checkCode = "";
char tmp = 0;
int x = -distance;
for (int i = 0; i < show; i++) {
tmp = str.charAt(random.nextInt(str.length()-1));
checkCode = checkCode + tmp;
x = x + distance;
d.setColor(new Color(random.nextInt(100)+50,random.nextInt(100)+50,random.nextInt(100)+50));
d.drawString(tmp+"",x,random.nextInt(image.getHeight()-(font.getSize()))+(font.getSize()));
}
d.dispose();
ImageIO.write(image,"jpeg",output);
return checkCode;
}
public String execute() throws Exception {
MyDebug.println("-------RandomAction-----------");
HttpServletResponse response = ServletActionContext.getResponse();
getSession().put("random",this.getCheckCodeImage("ABCDEFGHJKLMNPQRSTUVWXYZ123456789",4,response.getOutputStream()));
return SUCCESS;
}
}
struts配置文件中加入:
<package name="random" extends="struts-default">
<action name="random" class="com.cetc.struts.action.t00hy.RandomAction">
<result name="success">/pages/home/random.jsp</result>
</action>
</package>
random.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
out.clear();
out = pageContext.pushBody();
%>
register.jsp页面加入:
<tr>
<td class='labeltable_middle_td_01' align=right>验证码</td>
<td colspan="5" >
<s:textfield name="num" maxlength="4" cssClass="input" theme="simple" cssStyle="ime-mode:disabled;"/> <a title="看不清吗?换一张吧!" href='javascript:show(document.getElementById("random"))'><img src="<%=basePath%>random.action" id="random" border="0" /></a>
</td>
</tr>
js:
function show(o){
//重载验证码
var timenow = new Date().getTime();
o.src="<%=basePath%>random.action?d="+timenow;
/*
//超时执行;
setTimeout(function(){o.src="<%=basePath%>random.action?d="+timenow;},20);
*/
}
- struts2.0中使用图形验证码
- 【转】Struts2 图形验证码
- struts2中使用的验证码
- 图片验证码(Struts2中使用)
- struts2验证码使用
- struts2验证码使用
- struts2验证码使用
- struts2验证码使用
- 使用Struts2验证码
- struts2使用验证码
- struts2中验证码的生成和使用
- Struts2:自定义返回视图类型-返回图形验证码
- Struts2中实现验证码
- struts2中生成验证码
- jsp中生成图形验证码
- java 中生成图形验证码
- java 中生成图形验证码
- java 中生成图形验证码
- 正确配置和使用SQL mail
- 关于apache的url 重写
- 文件下载的制作
- 《三国演义》中必学的六大领导艺术
- Java的基本语法
- struts2.0中使用图形验证码
- WINDOWS消息经常使用的十六进制常数
- 列举当前正在被其他程序占用的文件
- 如何循序渐进向DotNet架构师发展(转载)
- Roller(Weblogic)
- c#中如何删除只读文件
- div跟随javasript
- 得到选中的radiobutton值
- 消防监控方案(ARM)