用java来实现验证码功能。

昨天在网上看到了一篇关于验证码的文章,很不错,但是有些不尽人意的地方,比如没有考虑到前端传过来的验证码如果是小写的话,那么做验证的时候就会出现错误,

因为java是严格区分大小写的,还有就是验证码会重叠在一起的情况没有考虑到,再者验证码看不清刷新的功能没有做出来

在此我做下修改:

首先需要导入ValidateCode.jar包

 1 package com.wqy.ValidateCode;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.imageio.ImageIO;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 import cn.dsna.util.images.ValidateCode;
12 
13 public class ValidateCode1 extends HttpServlet {
14 
15     public void doGet(HttpServletRequest request, HttpServletResponse response)
16             throws ServletException, IOException {
17         //控制不要缓存
18         response.setHeader("Expires", "-1");
19         response.setHeader("Cache-Control", "no-cache");
20         response.setHeader("Pragma", "no-cache");
21         int width=120;//宽度
22         int height=25;//高度
23         int codeCount=4;//验证码的个数
24         int lineCount=9;//干扰线的个数
25         //设置验证码
26         ValidateCode vc = new ValidateCode(width, height, codeCount, lineCount);
27                 //ValidateCode(120,25,4,9);
28         String code = vc.getCode();
29         //将验证码存放到Attribute域中
30         request.getSession().setAttribute("code", code);
31         //输出
32         ImageIO.write(vc.getBuffImg(), "jpg", response.getOutputStream());
33         
34     }
35 
36     public void doPost(HttpServletRequest request, HttpServletResponse response)
37             throws ServletException, IOException {
38             doGet(request,response);
39     }
40 
41 }

下面是html代码:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
  <head>
    <title>验证码</title>
  </head>
  
  <body>
      <form action="${pageContext.request.contextPath}/servlet/LoginServlet" method="post">
      用户名:<input type="text" name="username"/></br>
      密码:<input type="password" name="password"/></br>
      验证码:<input type="text" name="captcha" size="4">
      <img src="${pageContext.request.contextPath}/servlet/ValidateCode1"  id="CreateCheckCode" onclick="this.src=this.src+'?'" width="120" height="25">
      <a href="javascript:CreateCheckCode.onclick()">看不清,换一张</a></br>
      <input type="submit" value="登录">
      </form>
  </body>
</html>

后台验证的servlet代码:

 1 package com.wqy.ValidateCode;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class LoginServlet extends HttpServlet {
11     public void doGet(HttpServletRequest request, HttpServletResponse response)
12             throws ServletException, IOException {
13         //获取前端传来的验证码
14         String name = request.getParameter("captcha");
15         //从Attribute域中获取存放的验证码
16         String code = (String)request.getSession().getAttribute("code");
17         //前后端验证码做比较。(验证码.toLowerCase是让它忽略大小写,这样在前端做验证的时候就不会因为大小写问题引发异常)
18         if(code.toLowerCase().equals(name.toLowerCase())){
19             System.out.println("验证码正确");
20         }
21     }
22 
23     public void doPost(HttpServletRequest request, HttpServletResponse response)
24             throws ServletException, IOException {
25         doGet(request, response);
26     }
27 
28 }

最终显示结果:

原文地址:https://www.cnblogs.com/RunningSir/p/6979075.html