验证码的一些相关文件!!!!!!
1.《JQuery》记得加上文档就绪
$("#yzm").blur(function(){
var code=$(this).val();
if ($(this).val().length>0) {
$("#y").text("");
$.post(
"${pageContext.request.contextPath }/checkcaptcha",
"code="+code,
function(data){
$("#y").text(data.msg);
if (data.msg=="验证通过") {
$("#c").attr("color","green");
} else {
$("#c").attr("color","red");
}
},
"json"
);
} else {
$("#y").text("请输入验证码!");
}
});
2.标签(placeholder:“隐藏文字”,maxlength:“验证码的长度”,autocomplete:点击打开链接,title:点击打开链接)
<input id="yzm" placeholder="验证码" type="text" name="captcha" class="text captcha" maxlength="4" autocomplete="off" style="font-size:30px">
<img id="captcha" class="captcha" src="${pageContext.request.contextPath }/captcha" title="点击更换验证码"></span><br>
<font id="c" color='red'><span id="y"></span></font><br>
3.工具类
package com.LG.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* @ClassName: CaptchaUtil
* @Description: 关于验证码的工具类
* @author 无名
* @date 2016-5-7 上午8:33:08
* @version 1.0
*/
public final class CaptchaUtil
{
private CaptchaUtil(){}
/*
* 随机字符字典
*/
private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
/*
* 随机数
*/
private static Random random = new Random();
/*
* 获取6位随机数
*/
private static String getRandomString()
{
StringBuffer buffer = new StringBuffer();
for(int i = 0; i < 4; i++)
{
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
/*
* 获取随机数颜色
*/
private static Color getRandomColor()
{
return new Color(random.nextInt(255),random.nextInt(255),
random.nextInt(255));
}
/*
* 返回某颜色的反色
*/
private static Color getReverseColor(Color c)
{
return new Color(255 - c.getRed(), 255 - c.getGreen(),
255 - c.getBlue());
}
public static void outputCaptcha(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("image/jpeg");
String randomString = getRandomString();
//往HttpSession对象放验证码
request.getSession(true).setAttribute("randomString", randomString);
int width = 100;
int height = 30;
Color color = getRandomColor();
Color reverse = getReverseColor(color);
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
g.setColor(color);
g.fillRect(0, 0, width, height);
g.setColor(reverse);
g.drawString(randomString, 18, 20);
for (int i = 0, n = random.nextInt(100); i < n; i++)
{
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
}
// 转成JPEG格式
ServletOutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
}
}
4.Controller层
package com.LG.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yousian.util.CaptchaUtil;
@Controller
public class CaptchaController {
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception{
CaptchaUtil.outputCaptcha(request, response);
}
@RequestMapping("/checkcaptcha")
@ResponseBody
public String checkcaptcha(HttpSession session,String code){
String scode = (String) session.getAttribute("randomString");
if (code.equalsIgnoreCase(scode)) {
return "{\"msg\":\"验证通过\"}";
}
return "{\"msg\":\"验证失败\"}";
}
}
博主的博客中还有一些正则表达式(邮箱/手机号/QQ/密码)可以帮助大家借鉴!!!