struts2.0中使用图形验证码

产生随机数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);
*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值