昨天在网上看到了一篇关于验证码的文章,很不错,但是有些不尽人意的地方,比如没有考虑到前端传过来的验证码如果是小写的话,那么做验证的时候就会出现错误,
因为 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 classValidateCode1extends HttpServlet {
- 14
- 15 public void doGet(HttpServletRequest request, HttpServletResponse response)
- 16 throws ServletException, IOException {
- 17 //控制不要缓存
- 18response.setHeader("Expires", "-1");
- 19response.setHeader("Cache-Control", "no-cache");
- 20response.setHeader("Pragma", "no-cache");
- 21 intwidth=120;//宽度
- 22 intheight=25;//高度
- 23 intcodeCount=4;//验证码的个数
- 24 intlineCount=9;//干扰线的个数
- 25 //设置验证码
- 26ValidateCode vc =new ValidateCode(width, height, codeCount, lineCount);
- 27 //ValidateCode(120,25,4,9);
- 28String code = vc.getCode();
- 29 //将验证码存放到Attribute域中
- 30request.getSession().setAttribute("code", code);
- 31 //输出
- 32ImageIO.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=r equest.getContextPath(); String basePath=r equest.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 classLoginServletextends HttpServlet {
- 11 public void doGet(HttpServletRequest request, HttpServletResponse response)
- 12 throws ServletException, IOException {
- 13 //获取前端传来的验证码
- 14String name = request.getParameter("captcha");
- 15 //从Attribute域中获取存放的验证码
- 16String code = (String)request.getSession().getAttribute("code");
- 17 //前后端验证码做比较。(验证码.toLowerCase是让它忽略大小写,这样在前端做验证的时候就不会因为大小写问题引发异常)
- 18 if(code.toLowerCase().equals(name.toLowerCase())){
- 19System.out.println("验证码正确");
- 20 }
- 21 }
- 22
- 23 public void doPost(HttpServletRequest request, HttpServletResponse response)
- 24 throws ServletException, IOException {
- 25 doGet(request, response);
- 26 }
- 27
- 28}
最终显示结果:
来源: http://www.cnblogs.com/RunningSir/p/6979075.html