当前位置: 移动技术网 > IT编程>开发语言>Java > java 制作验证码并进行验证实例详解

java 制作验证码并进行验证实例详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

java 制作验证码并进行验证实例详解

在注册、登录的页面上经常会出现验证码,为了防止频繁的注册或登录行为。下面是我用java制作的一个验证码,供初学者参考,做完验证码之后,我们可以用ajax进行验证码验证。

功能一:验证码制作的代码,点击图片,验证码进行更换

/**
 * 显示验证码图片
 */
public void showcheckcode(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
  // 调用业务逻辑
  string checkcode = getcheckcode();
  //将验证码字符放入session域对象中
  req.getsession().setattribute("checkcode", checkcode);

  //图片宽
  int width = 80;
  //图片高
  int height = 30;
  //在内存中创建一个图片
  bufferedimage image = new bufferedimage(width,height,bufferedimage.type_int_rgb);
  //获取一个画笔
  graphics g = image.getgraphics();
  //设置画笔颜色,用灰色做背景
  g.setcolor(color.gray);
  //向image中填充灰色
  g.fillrect(0,0,width,height);

  random r = new random();

  //设置3条干扰线
  for (int i = 0; i < 3; i++) {
    g.setcolor(new color(r.nextint(255),r.nextint(255),r.nextint(255)));
    g.drawline(r.nextint(80), r.nextint(30), r.nextint(80), r.nextint(80));
  }

  //设置验证码字符串的颜色
  g.setcolor(new color(r.nextint(255),r.nextint(255),r.nextint(255)));
  //设置字符的大小
  g.setfont(new font("黑体",font.bold,24));
  //在图片中写入验证码字符串
  g.drawstring(checkcode,15,20);
  //将image对象以png格式输出给所有的客户端
  imageio.write(image,"png",resp.getoutputstream());
}

/**
 * 获取4位验证码中的4位随机字符串
 */
public static string getcheckcode(){
  //验证码中的字符由数字和大小写字母组成
  string code = "0123456789qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm";
  random r = new random();
  stringbuffer sb = new stringbuffer();
  for (int i = 0; i < 4; i++) {
    sb.append(code.charat(r.nextint(code.length())));
  }

  return sb.tostring();
}

jsp页面

<script type="text/javascript">
  function changecodeimage(img){
    img.src = "${pagecontext.request.contextpath}/userservlet?method=showcheckcode&time="+new date().gettime();
  }

</script>

 <div class="form-group">
  <label for="date" class="col-sm-2 control-label">验证码</label>
  <div class="col-sm-3">
   <input type="text" class="form-control" id="writecode" onkeyup="checkcodemethod(this.value)" >

  </div>
  <div class="col-sm-2">
  <img src="${pagecontext.request.contextpath}/userservlet?method=showcheckcode" id="checkcodeimage" title="点击换一张" onclick="changecodeimage(this)" />
  </div>
  <span id="checkcodespan"></span>
 </div>

功能二:ajax动态验证验证码

/**
 * 验证验证码
 */
public void checkcode(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {

  //获取从页面中接收到的验证码参数
  string checkcode = req.getparameter("checkcode");
  //从session域对象中获取验证码
  string sessioncode = (string) req.getsession().getattribute("checkcode");
  //判断验证码是否相同
  if (checkcode.equalsignorecase(sessioncode)) {
    resp.getwriter().print(true);
  }else {
    resp.getwriter().print(false);
  }

jsp页面

<script type="text/javascript">
  function changecodeimage(img){
    img.src = "${pagecontext.request.contextpath}/userservlet?method=showcheckcode&time="+new date().gettime();
  }

  function checkcodemethod(code){
    $.get("${pagecontext.request.contextpath}/userservlet?method=checkcode", 
        { checkcode: code}, 
        function(data){
          if (data == 'true') {
            document.getelementbyid("checkcodespan").innerhtml = "<font>验证码正确!</font>";
          }else {
            document.getelementbyid("checkcodespan").innerhtml = "<font>验证码错误!</font>";
          }
        }
      );
  }

</script>

 <div class="form-group">
  <label for="date" class="col-sm-2 control-label">验证码</label>
  <div class="col-sm-3">
   <input type="text" class="form-control" id="writecode" onkeyup="checkcodemethod(this.value)" >

  </div>
  <div class="col-sm-2">
  <img src="${pagecontext.request.contextpath}/userservlet?method=showcheckcode" id="checkcodeimage" title="点击换一张" onclick="changecodeimage(this)" />
  </div>
  <span id="checkcodespan"></span>
 </div>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网