继续总结与编程相关的技术,今天总结的是大学期间关于验证码技术的一个小示例:
代码1——生成验证码的jsp文件,文件名:captcha.jsp:
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*,java.io.*" %> <html> <body> <%! 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; int height=20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); graphics.setColor(getRandColor(200,250)); graphics.fillRect(0, 0, width, height); graphics.setFont(new Font("Times New Roman",Font.PLAIN,18)); graphics.setColor(getRandColor(160,200)); graphics.drawRect(0,0,width-1,height-1); Random random = new Random(); for (int i=0;i<155;i++){ int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); graphics.drawLine(x,y,x+xl,y+yl); } String sRand=""; for (int i=0;i<4;i++){ String rand=String.valueOf(random.nextInt(10)); sRand+=rand; graphics.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); graphics.drawString(rand,13*i+6,16); } session.setAttribute("rand",sRand); graphics.dispose(); ImageIO.write(image,"JPEG",response.getOutputStream()); out.clear(); out = pageContext.pushBody(); %> </body> </html>代码2——使用验证码的jsp文件,文件名:index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"; %> <html> <head> <title>验证码的使用</title> <script type="text/javascript"> function reloadImage(){ document.getElementById("safecode").src="captcha.jsp?seed"+Math.random(); return false; } function checkYanZhengMa(){ var value = document.getElementById("yanzhengma").value;//获取用户在“验证码”文本框中输入的验证码。 if(value==""){ document.getElementById("showYanZhengMa").innerHTML="<font color='red' size='2'>验证码不能为空!!!</font>"; } else{ var xmlHttpRequest = null; if(window.XMLHttpRequest){//适用于IE7以上(包括IE7)的IE浏览器、火狐浏览器、谷歌浏览器和Opera浏览器 xmlHttpRequest = new XMLHttpRequest();//创建XMLHttpRequest }else if(window.ActiveXObject){//适用于IE6.0以下(包括IE6.0)的IE浏览器 xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }//第一步,每次请求都需要重新创建一个XMLHttpRequest对象 xmlHttpRequest.open("post","./AjaxCheckCaptchaServlet",true);//第二步,open方法中的三个参数分别是:请求方式、路径、是否异步(true表示异步,false表示同步) xmlHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttpRequest.send("yanzhengma_value="+value);//第三步 发送请求 xmlHttpRequest.onreadystatechange=listeningReadyState;//第四步 监听 处理响应后的数据 ,注意状态是4时处理 function listeningReadyState(){ if(xmlHttpRequest.readyState==4){ var responseValue=xmlHttpRequest.responseText; if(responseValue==1){ document.getElementById("showYanZhengMa").innerHTML="<font color='green' size='2'>通过验证!!!</font>"; }else if(responseValue==2){ document.getElementById("showYanZhengMa").innerHTML="<font color='red' size='2'>验证码输入错误!!!</font>"; } } } } } </script> </head> <body> <input type="text" id="yanzhengma" name="yanzhengma" size="6" onblur="checkYanZhengMa();"> <img name="safecode" id="safecode" src="./captcha.jsp" onclick="reloadImage();"/> <font color="#000000" size="4"><strong><a href="#" onclick="reloadImage();">换一张</a></strong></font> <font size="2" id="showYanZhengMa"> *请正确填写验证码。</font> </body> </html>代码3——异步校验验证码的java文件,文件名:AjaxCheckCaptchaServlet:
package com.ghj.packageofservlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class AjaxCheckCaptchaServlet extends HttpServlet { private static final long serialVersionUID = 6312962948471033343L; public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); HttpSession session=request.getSession(); String yanzhengma=request.getParameter("yanzhengma_value");//获取用户在“验证码”文本框中输入的验证码,引号中的值为引号传参中的key。 System.out.println("异步验证————用户输入的验证码是:"+yanzhengma); String rand = ((String)session.getAttribute("rand")).trim();//得到原验证码,"rand"来自captcha.jsp System.out.println("异步验证————原验证码是:"+rand); if (rand.equals(yanzhengma)) { out.print("1");//“1”表示用户输入的验证码和原验证码一致。 } else { out.print("2");//“2”表示用户输入的验证码和原验证码不一致。 } out.flush(); out.close(); } }代码4——配置Servlet的xml文件,文件名:web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>AjaxCheckCaptchaServlet</servlet-name> <servlet-class>com.ghj.packageofservlet.AjaxCheckCaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AjaxCheckCaptchaServlet</servlet-name> <url-pattern>/AjaxCheckCaptchaServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
演示说明:将验证码输入到文本框后,请让该文本框失去焦点,程序采用异步的方式校验所输入的验证码和原来生成的验证码是否一致。
【0分下载资源】