首页 > 代码库 > 验证码(摘)
验证码(摘)
1、前台的代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘pic-login.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> function reload(){ document.getElementById("image").src="<%=request.getContextPath() %>/ImageServlet?date="+new Date().getTime(); $("#checkcode").val(""); // 将验证码清空 } function verificationcode(){ var text=$.trim($("#checkcode").val()); $.post("${pageContext.request.contextPath}/VerificationServlet",{op:text},function(data){ data=parseInt($.trim(data)); if(data>0){ $("#span").text("验证成功!").css("color","green"); }else{ $("#span").text("验证失败!").css("color","red"); reload(); //验证失败后需要更换验证码 $("#checkcode").val(""); // 将验证码清空 } }); } </script> </head> <body> 验证码:<input type="text" name="checkcode" id="checkcode" onchange="javascript:verificationcode()"/> <img src="<%=request.getContextPath() %>/ImageServlet" title="点击换一张" id="image" onclick="javascript:reload()"/> <span id="span"></span> </body> </html>
2、这里图片的src是一个servlet-ImageServlet.java
package com.java.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/ImageServlet") public class ImageServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ImageServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //在内存中创建图象 int width = 80; int height = 25; BufferedImage bfi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //创建图象 Graphics g = bfi.getGraphics(); g.fillRect(0, 0, width, height); //验证码字符范围 char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray(); Random r = new Random(); int index; StringBuffer sb = new StringBuffer(); //保存字符串 //随机产生认证码 for(int i=0; i<4; i++){ index = r.nextInt(ch.length); g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255))); Font font = new Font("Tines Nev Roman", 30, 20); g.setFont(font); g.drawString(ch[index]+"", (i*20)+2, 23); sb.append(ch[index]); } // 添加噪点 int area = (int) (0.02 * width * height); for(int i=0; i<area; ++i){ int x = (int)(Math.random() * width); int y = (int)(Math.random() * height); bfi.setRGB(x, y, (int)(Math.random() * 255)); } //设置验证码中的干扰线 for (int i = 0; i < 1; i++) { //随机获取干扰线的起点和终点 int xstart = (int)(Math.random() * width); int ystart = (int)(Math.random() * height); int xend = (int)(Math.random() * width); int yend = (int)(Math.random() * height); g.setColor(interLine(1, 255)); g.drawLine(xstart, ystart, xend, yend); } HttpSession session = request.getSession(); //保存到session session.setAttribute("verificationCode", sb.toString()); ImageIO.write(bfi, "JPG", response.getOutputStream()); //写到输出流 } private static Color interLine(int Low, int High){ if(Low > 255) Low = 255; if(High > 255) High = 255; if(Low < 0) Low = 0; if(High < 0) High = 0; int interval = High - Low; int r = Low + (int)(Math.random() * interval); int g = Low + (int)(Math.random() * interval); int b = Low + (int)(Math.random() * interval); return new Color(r, g, b); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
这里使用ImageServlet生成图片验证码,使用post后台验证,在点击标签换验证码图片的时候,一定要在调用生成图片前加一个时间戳,也就是上面的document.getElementById("image").src="http://www.mamicode.com//imageServlet?date="+new Date().getTime();这句代码的最后加?后面的代码一定不能少,因为浏览器是由缓存的,如果不加时间戳,在点击标签换图片时,验证码图片不会正常更换。
为了便于比对结果,一定要将字符串保存到session。因为session是当前会话有效的,也就是说在你关闭浏览器之前,session里的值都是可以取到的。这里为了增加验证的难度,是不区分大小写的。噪点和干扰线的数量也是可调的。
3、最后是使用VerificationServlet对验证码图片上的字符与输入的字符串进行比对!把存入session中的verificationCode取出来与checkcode进行比对,返回结果
package com.java.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/VerificationServlet") public class VerificationServlet extends HttpServlet { private static final long serialVersionUID = 1L; public VerificationServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session =request.getSession(); String verificationCode = (String)session.getAttribute("verificationCode"); String checkcode = request.getParameter("op"); PrintWriter out = response.getWriter(); if(checkcode.equals(verificationCode)){ out.println(1); }else{ out.println(0); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
4、效果
摘自:http://blog.csdn.net/weixian52034/article/details/52186207
验证码(摘)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。