直接上源码。。。()
<div class="formText2">
<div><input type="type" id="code" maxlength="4" class="text" autocomplete="off" style="ime-mode:disabled" size="4"/>
<img onclick="change()" height="30px" alt="看不清,换一张" id="myimg" src="${path}/mast/code/getCode.action"></img>
<p class="placeholder">请输入验证码</p>
</div>
function login() {
var datas = {"codes": $("#code").val()};
var s="1";
$.ajax({
url:"${path}/mast/code/judCode.action",
async:false,
data : datas,
success:function(result){
if(result != undefined && result != "0"){
if(result=="2"){
s="2";
return;
}
}else{
alert("发送失败!");
return 0;
}
}
});
if(s=="2"){
alert("验证码错误");
var verifyObj = document.getElementById("myimg");
verifyObj.src="${path}/mast/code/getCode.action?timestamp="+Math.random();
return;
}
}
}
function change(){
var verifyObj = document.getElementById("myimg");
verifyObj.src="${path}/mast/code/getCode.action?timestamp="+Math.random();
}
@Namespace("")
@ResultPath(value="/")
public class CodeAction extends BaseAction {
private String temp;
private String codes;
private String bz;
public String getBz() {
return bz;
}
public void setBz(String bz) {
this.bz = bz;
}
public String getCodes() {
return codes;
}
public void setCodes(String codes) {
this.codes = codes;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public static final Integer NUM = 4;
@Action(value="getCode", results={
@Result(name="success",location="/index.jsp")
})
public void getCode() throws IOException{
ActionContext context = ActionContext.getContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();;
response.addHeader("pragma", "no-cache");
response.addHeader("cache-control", "no-cache");
response.addHeader("expires", "0");
//System.out.println("image");
char[] rands = getRandom();
String temp = new String(rands);
request.getSession().setAttribute("temp", temp);
//产生图片缓冲区
BufferedImage bufferedImage = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
//得到缓图对象
Graphics g = bufferedImage.getGraphics();
//设置背景色
g.setColor(Color.lightGray);
//填充
g.fillRect(0, 0, 80, 20);
//写字
g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.BOLD,28));
g.drawString(rands[0]+"", 0, 18);
g.drawString(rands[1]+"", 20, 15);
g.drawString(rands[2]+"", 40, 19);
g.drawString(rands[3]+"", 60, 16);
//四根线
Random r = new Random();
g.setColor(Color.pink);
g.drawLine(r.nextInt(80), r.nextInt(20),r.nextInt(80), r.nextInt(20));
g.drawLine(r.nextInt(80), r.nextInt(20),r.nextInt(80), r.nextInt(20));
g.drawLine(r.nextInt(80), r.nextInt(20),r.nextInt(80), r.nextInt(20));
g.drawLine(r.nextInt(80), r.nextInt(20),r.nextInt(80), r.nextInt(20));
//生成jpeg
ImageIO.write(bufferedImage, "jpeg", response.getOutputStream());
this.temp=temp;
}
@Action(value="judCode", results={
@Result(name="success",location="/index.jsp")
})
public void judCode(){
String code=getCodes();
String temp = (String) ActionContext.getContext().getSession().get("temp");
String bz="0";
if(code.equalsIgnoreCase(temp)){
bz="1";
}else{
bz="2";
}
returnPopupMessage(true ? bz : "0");
}
//产生四个随机数
public char[] getRandom()
{
//源始的内容
String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
//存放随机数
char[] rands = new char[NUM];
Random random = new Random();
for (int i=0;i<NUM;i++)
{
int index = random.nextInt(62);
rands[i] = str.charAt(index);
}
return rands;
}
}