欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp+ajax实现的局部刷新较验验证码(onblur事件触发较验)

程序员文章站 2023-01-25 14:54:21
本文实例讲述了jsp+ajax实现的局部刷新较验验证码(onblur事件触发较验)。分享给大家供大家参考,具体如下: 前台显示页面: welcome.jsp...

本文实例讲述了jsp+ajax实现的局部刷新较验验证码(onblur事件触发较验)。分享给大家供大家参考,具体如下:

前台显示页面:

welcome.jsp

<%@ page language="java" contenttype="text/html; utf-8" 
 pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>修改个人信息</title>
<script>
function createxmlhttprequest() {
 //表示当前浏览器不是ie,如ns,firefox
 if(window.xmlhttprequest) {
  xmlhttp = new xmlhttprequest();
 } else if (window.activexobject) {
  xmlhttp = new activexobject("microsoft.xmlhttp");
 }
}
function validate(field) {
 var div=document.getelementbyid("divbid");
// var reg=/[^a-za-z0-9_]{7,10}/g;
 if (field.value.length != 0) {
  //创建xmlhttprequest
  createxmlhttprequest();
  var url = "codevalidate.jsp?authcode=" + field.value + "×tamp=" + new date().gettime();//加时间戳防止缓存重复
  xmlhttp.open("get", url, true);
  //方法地址,处理完成后自动调用,回调
  xmlhttp.onreadystatechange=function() { //匿名函数
   if(xmlhttp.readystate == 4) { //ajax引擎初始化成功
    if (xmlhttp.status == 200) { //http协议成功
     document.getelementbyid("divbid").innerhtml =xmlhttp.responsetext;
    }else {
     alert("请求失败,错误码=" + xmlhttp.status);
    }
   }
  };
  //将参数发送到ajax引擎
  xmlhttp.send(null);
 }else {
   div.innerhtml="<div class=\"wrong\">验证码不能为空</div>"
 }
}
function reloadimage(imgurl){
 var getimagecode=document.getelementbyid("codeimg");
 getimagecode.src= imgurl + "?id=" + math.random();
  var code=<%=session.getattribute("code")%>;
  alert(code)
}
</script>
</head>
<body>
<form action="authcode.jsp" method="post" name="loginform">
  输入验证码:<input type="text" name="authcode" id="authcode" maxlength="4" size="10" onblur="validate(this)" ><span id="divbid"></span>
  <img id="codeimg" name="codeimg" border=0 src="authcode.jsp">
  <a href="javascript:reloadimage('authcode.jsp')">看不清</a><br/>
</form>
<script language="javascript" type="text/javascript">
function reloadimage(imgurl){
 var getimagecode=document.getelementbyid("codeimg");
 getimagecode.src= imgurl + "?id=" + math.random();
 var code=<%=session.getattribute("code")%>;
 alert(code)
}
</script>
</body>
</html>

产生图片页面

authcode.jsp

<%@ page contenttype="image/jpeg" 
 import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" 
 pageencoding="gbk"%> 
<%!color getrandcolor(int fc, int bc) {//给定范围获得随机颜色  
  random random = new random();  
  if (fc > 255)  
   fc = 255;  
  if (bc > 255)  
   bc = 255;  
  int r = fc + random.nextint(bc - fc);  
  int g = fc + random.nextint(bc - fc);  
  int b = fc + random.nextint(bc - fc);  
  return new color(r, g, b);  
 }%> 
<%  
 //设置页面不缓存  
 response.setheader("pragma", "no-cache");  
 response.setheader("cache-control", "no-cache");  
 response.setdateheader("expires", 0);  
 // 在内存中创建图象  
 int width = 60, height = 20;  
 bufferedimage image = new bufferedimage(width, height,  
   bufferedimage.type_int_rgb);  
 // 获取图形上下文  
 graphics g = image.getgraphics();  
 //生成随机类  
 random random = new random();  
 // 设定背景色  
 g.setcolor(getrandcolor(200, 250));  
 g.fillrect(0, 0, width, height);  
 //设定字体  
 g.setfont(new font("times new roman", font.plain, 18));  
 //画边框  
 //g.setcolor(new color());  
 //g.drawrect(0,0,width-1,height-1);  
 // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到  
 g.setcolor(getrandcolor(160, 200));  
 for (int i = 0; i < 100; i++) {  
  int x = random.nextint(width);  
  int y = random.nextint(height);  
  int xl = random.nextint(12);  
  int yl = random.nextint(12);  
  g.drawline(x, y, x + xl, y + yl);  
 }  
 // 取随机产生的认证码(4位数字)  
 string srand = ""; 
 for (int i = 0; i < 4; i++) {  
  string rand = string.valueof(random.nextint(10));  
  srand += rand;  
  // 将认证码显示到图象中  
  g.setcolor(new color(20 + random.nextint(110), 20 + random  
  .nextint(110), 20 + random.nextint(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成  
  g.drawstring(rand, 13 * i + 6, 16);  
  system.out.println(rand); 
 }  
 // 将认证码存入session  
 session.setattribute("code", srand);  
 // 图象生效  
 g.dispose();  
 // 输出图象到页面  
 imageio.write(image, "jpeg", response.getoutputstream());  
%>

ajax动态验证页面

codevalidate.jsp

<%@ page language="java" import="java.util.*,java.util.*,java.text.*,com.neusoft.xkxt.dao.*,com.neusoft.xkxt.bean.*,com.neusoft.xkxt.util.*" pageencoding="utf-8"%> 
<% 
string path = request.getcontextpath(); 
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; 
%> 
<% 
 string authcode=request.getparameter("authcode"); 
 string code=(string)session.getattribute("code"); 
 if(authcode.equals(code)){ 
 out.println("<div class='right'>√</div>");} 
 else{ 
 out.println("<div class='wrong'>验证码不正确</div>");} 
%>

希望本文所述对大家jsp程序设计有所帮助。