代碼如下,
1,img.jsp,輸入主頁面
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" pageEncoding="GBK"%>
<%
//set Chinese Char
//Cody by JarryLi@gmail.com;
//homepage:jiarry.126.com
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("text/html; charset=GBK");
%>
<html>
<head>
<title>圖片驗證</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script src="net.js"></script>
</head>
<body>
AJAX(無刷新及時提示)驗證碼實例!cody by jarry
<hr>
<%
String num=request.getParameter("num");
String random=(String)session.getAttribute("random");
String name=request.getParameter("name");
if(num!=null&&random!=null&&name!=null)
{
if(num.equals(random))
{
out.println("<font style=\"color:green;font-weight:bold\">恭喜您,驗證碼輸入成功,這裡是提交結果頁面,可以寫入數據庫了!</font> <a href=\"img.jsp\">返回再測試</a><br>");
out.println("您的名字是:"+name);
out.println("<br>");
out.println("您輸入的是:"+num);
out.println("驗證碼是:"+random);
out.println("</body>");
return;//javascript:history.go(-1)
}
}
%>
<script type="text/javascript">
var times=0;
function subform(){
var gtext=this.req.responseText;
var info=document.getElementById("info");
if(gtext.indexOf("validate_successful")!=-1){
//info.innerHTML="<font color=green>驗證碼通過</font>";
document.forms["myform"].submit();
//當得到的值表示合法,則驗證碼通過。
}else{
times++;
if(times>=3){//如果連接3次輸入錯誤,則重載圖片,可以防止作弊和用戶看不清圖片;
info.innerHTML="接連3次輸入錯誤。更新驗證碼,請重新輸入";
document.forms["myform"].num.value="";
show(document.getElementById('random'));
times=0;
}else{
info.innerHTML="第"+times+"次驗證碼錯誤,請注意區分大小寫 ";
}
document.forms["myform"].num.select();
}
}
function validata(obj){
var enter=true;
var info=do