您的位置:首页 > 编程语言 > Java开发

Form表单生成数字、字母验证码_Ajax--------struts2

2017-07-27 15:44 295 查看
生成4位纯数字验证码

生成4位纯大写字母验证码

生成4位纯小写字母验证码

生成4位纯字母(大/小写)验证码

生成4位数字、字母验证码

[源码下载地址:http://download.csdn.net/detail/yan13507001470/9912631]

部分代码如下

生成4位数字、字母验证码
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="image/jpeg"  import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
public Color getColor(){
Random random = new Random();
int r = random.nextInt(256);//0-255
int g = random.nextInt(256);
int b = random.nextInt(256);
return new Color(r,g,b); //产生随机颜色
}
/* 生成4位数字、字母验证码  */
public String getNum(){
String str = "";
for (int i = 0; i < 4; i++) {
int num = (int)(Math.random() * 3 + 1) ;
if(num == 1){
str = str + (char) (Math.random() * 26 + 'a');
}else if(num ==2 ){
str = str + (char) (Math.random() * 26 + 'A');
}else{
str += (int)(Math.random()*10);//0-9
}
}
return str;
}
%>
<%
//清除缓存
response.
4000
setHeader("pragma", "mo-cache");
response.setHeader("cache-control", "no-cache");
response.setDateHeader("expires", 0);
//产生矩形框
BufferedImage image = new BufferedImage(80,30,BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();
g.setColor(new Color(200,200,200));//数字颜色
g.fillRect(0,0,80,30);

//产生线段
for (int i = 0; i < 50; i++) {
Random random = new Random();
int x = random.nextInt(80);
int y = random.nextInt(30);
int xl = random.nextInt(x+10);
int yl = random.nextInt(y+10);
g.setColor(getColor());
g.drawLine(x, y, x + xl, y + yl);
}

//字体大小、背景
g.setFont(new Font("serif", Font.BOLD,18));
g.setColor(Color.BLACK);
String checkNum = getNum();//"1010"

StringBuffer sb = new StringBuffer();
for(int i=0;i<checkNum.length();i++){
sb.append(checkNum.charAt(i)+" ");//"1 0 1 0"
}
//写入
g.drawString(sb.toString(),10,20);

session.setAttribute("CHECKNUM",checkNum);//1010

ImageIO.write(image,"jpeg",response.getOutputStream());
out.clear();
out = pageContext.pushBody();
%>


**Jsp**
<body>
<h4 style="color:red;">不区分大小写</h4>
<form action="http://www.baidu.com" method="get">
<table>
<tr>
<td>验证码:</td>
<td>
<input type="text" name="checkcode" id="checkcodeId" size="2" maxlength="4">
<a href="javascript:change()"><img id="code" alt="看不清除,换一张?" src="checkCode.jsp"></a>
</td>
<td id="res"></td>
</tr>
<tr>
<td colspan="2"><input id="buttonSubmit" type="button" value="login"></td>
</tr>
</table>
</form>
<script type="text/javascript">
function change(){
document.getElementById("code").src="checkCode.jsp?time="+new Date().getTime();
}
</script>
<script type="text/javascript">
//去掉左右空格
function trim(str){
str = str.replace(/^\s*/,"");
str = str.replace(/\s*$/,"");
return str;
}
</script>
<script type="text/javascript">
document.getElementById("checkcodeId").onkeyup = function(){
var checkcode = this.value;
checkcode = trim(checkcode);
if(checkcode.length == 4){
var ajax = createAJAX();
method="POST";
url="${pageContext.request.contextPath}/checkRequest_check?time="+new Date().getTime();
ajax.open(method, url);
ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
var content = "checkcode="+checkcode;
ajax.send(content);

ajax.onreadystatechange = function(){
if(ajax.readyState == 4){
if(ajax.status == 200){
var tip = ajax.responseText;    //获取校验结果 right OR error
//alert(tip)
if(tip == "right"){
tip='img/right.png';
//当验证码正确时,方可提交
document.getElementById("buttonSubmit").setAttribute("onclick", "this.form.submit()");
}else{
tip='img/error.png';
document.getElementById("buttonSubmit").setAttribute("onclick", "");
}

var img = document.createElement("img");
img.src = tip;
img.style.width = "16px";
img.style.height = "16px";
var td = document.getElementById("res");
td.innerHTML = "";
td.appendChild(img);
}
}
}
}else{
var td = document.getElementById("res");
td.innerHTML = "";
}
}
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: