登录验证码
1. 图片类的创建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.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//产生图片的servlet
public class VerifyCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private int width= 120;
private int height= 30;
private int codeCount= 4;
private int xx = 0;
private int fontHeight;
private int codeY;
char[] codeSequence= { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 定义图像的buffer
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D gd = buffImg.createGraphics();
// 创建一个随机数生成器类
Random random = new Random();
// 将图像填充为白色
gd.setColor(Color.white);
gd.fillRect(0, 0, width, height);
// 创建字体
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
gd.setFont(font);
// 画边框
gd.setColor(Color.black);
gd.drawRect(0, 0, width - 1, height - 1);
// 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
/* gd.setColor(Color.white);
for (int i = 0; i < 160; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
gd.drawLine(x, y, x + x1, y + y1);
}*/
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
gd.setColor(new Color(red, green, blue));
gd.drawString(strRand, (i + 1) * xx, codeY);
// 将产生的四个随机数组合在一起
randomCode.append(strRand);
}
// 将四位数字的验证码保存到Session中
HttpSession session = req.getSession();
session.setAttribute("validateCode", randomCode.toString());
// 禁止图像缓存
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
// 将图像输出到Servlet输出流中
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
@Override
public void init() throws ServletException {
// 从web.xml中获取初始信息
String strWidth = this.getInitParameter("width");
String strHeight = this.getInitParameter("height");
String strCodeCount = this.getInitParameter("codeCount");
// 将配置的信息转换成数值
try {
if (null != strWidth && 0 != strWidth.length()) {
width = Integer.parseInt(strWidth);
}
if (null != strHeight && 0 != strHeight.length()) {
height = Integer.parseInt(strHeight);
}
if (null != strCodeCount && 0 != strCodeCount.length()) {
codeCount = Integer.parseInt(strCodeCount);
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
xx = width / (codeCount + 1);
fontHeight = height - 2;
codeY = height - 4;
}
}
2.验证码判断的类的创建
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;
public class ResultServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String validateC= (String)request.getSession().getAttribute("validateCode");
String veryCode = request.getParameter("code");
PrintWriter out=response.getWriter();
if(veryCode == null || "".equals(veryCode)){
out.println("验证码为空");
}else{
if(validateC.equals(veryCode)){
out.println("验证码正确");
}
else{
out.println("验证码错误");
}
}
out.flush();
out.close();
}
}
3.verifyCode.jsp文件的创建
<%@ pagecontentType="text/html;charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script language="javascript" src="/xiao/resources/js/jquery.js"></script>
<script language="javascript" src="/xiao/resources/js/verifyCode.js"></script>
<title>test verify code</title>
</head>
<body>
<input id="veryCode" name="veryCode" type="text" />
<img id="imgObj" src="/xiao/verifyCode.jpg" />
<a href="#" onclick="changeImg()">换一张</a>
<input type="button" value="验证" onclick="isRightCode()">
<div id="info" />
</body>
</html>
4.verifyCode.js文件的创建
function changeImg(){
var timenow = new Date().getTime();
document.getElementById("imgObj").src="/xiao/verifyCode.jpg?timenow="+timenow;
}
function isRightCode(){
var code=$("#veryCode").val();
$.ajax({
url:"/xiao/resultServlet.result",
type:"POST",
data:{
code:code
},
success:callback
});
}
function callback(data){
$("#info").html(data);
}
4. web.xml 文件的配置
<!-- 验证码 -->
<servlet>
<servlet-name>VerifyCodeServlet</servlet-name>
<servlet-class>org.mm.xiao.VerifyCodeServlet</servlet-class>
<init-param>
<param-name>width</param-name>
<param-value>200</param-value>
</init-param>
<init-param>
<param-name>height</param-name>
<param-value>50</param-value>
</init-param>
<init-param>
<param-name>codeCount</param-name>
<param-value>5</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>VerifyCodeServlet</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ResultServlet</servlet-name>
<servlet-class>org.mm.xiao.ResultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ResultServlet</servlet-name>
<url-pattern>*.result</url-pattern>
</servlet-mapping>
--------------------编程问答-------------------- --------------------编程问答--------------------
来接分的啊。傻了吧。 --------------------编程问答-------------------- 这是公布java源码么? --------------------编程问答-------------------- --------------------编程问答-------------------- 验证码还是挺好做的 --------------------编程问答-------------------- --------------------编程问答-------------------- 我是来接分的 --------------------编程问答-------------------- 接分。。。。。。。。 --------------------编程问答-------------------- 什么意思?楼主是给源代码的么 --------------------编程问答-------------------- 谢谢楼主 接分 --------------------编程问答-------------------- 源代码还不错,看代码验证码好像只是数字吧 --------------------编程问答-------------------- 这是在问问题还是在分享代码啊
补充:Java , Web 开发