以下是实现思路:
1.在jsp页面用js生成验证码并保存在变量中,验证码作为参数传到java(继承了HttpServlet)类
2.根据jsp页面中传过来的验证码画出一个有干扰线或干扰点的图片
3.在WEB-INF/web.xml中配置 servlet
4.在jsp页面调用配置好的servlet,一般是通过<img src="servlet名" id="code"/>调用
5.提交数据之前验证
<form onsubmit="return validate()"……/>
局部刷新验证码:<a href="#" onclick="javascript:show()return false">看不清,换一张!</a>-
最简单的方法你把图一的显示验证码也弄成一个图片,比如叫code.png,上面你的代码就能换成<img src="code.png" onclick="this.src ={$maccms.path_tpl}picture/captcha.png"
<html><head>
<script language="javascript" type="text/javascript">
var code
function createCode(){
code = new Array()
var codeLength = 4
var checkCode = document.getElementById("checkCode")
checkCode.value = ""
var selectChar = new Array(2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z')
for(var i=0i<codeLengthi++) {
var charIndex = Math.floor(Math.random()*32)
code +=selectChar[charIndex]
}
checkCode.value = code
}
function validate () {
var inputCode = document.getElementById("yzm").value.toUpperCase()
if(inputCode != code ){
alert("验证码错误!")
return false
}
else {
alert("验证码正确!")
return true
}
}
</script>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
</head>
<body onLoad="createCode()">
验证码:<input type="text" id="yzm" size="5" />
<input type="button" id="checkCode" onClick="createCode()" title="刷新验证码" style="width:50pxcolor:#F00border:0letter-spacing:1pxfont-family:Arial" />
<input type="button" id="Button1" onClick="validate()" value="确定" />
</body>
</html>