java代码:
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class SecurityCode extends HttpServlet {
/**
* 产生验证码图片
*
* @return
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0L);
// HttpSession session = request.getSession(true);
int width = 90;
int height = 36;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景颜色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
// 画边框
g.setColor(Color.black);
g.drawRect(0, 0, width - 1, height - 1);
String strEnsure = request.getParameter("code");
Random rand = new Random();
// 将认证码显示到图像中,如果要生成更多位的验证码,增加drawString语句
g.setColor(getColor());
g.setFont(new Font("Atlantic Inline", 3 - rand.nextInt(3), 20));
String str = strEnsure.substring(0, 1);
g.drawString(str, 8, 28 - rand.nextInt(13));
str = strEnsure.substring(1, 2);
g.setColor(getColor());
g.setFont(new Font("Atlantic Inline", 3 - rand.nextInt(3), 20));
g.drawString(str, 25, 28 - rand.nextInt(13));
g.setColor(getColor());
g.setFont(new Font("Atlantic Inline", 3 - rand.nextInt(3), 20));
str = strEnsure.substring(2, 3);
g.drawString(str, 40, 28 - rand.nextInt(13));
str = strEnsure.substring(3, 4);
g.setColor(getColor());
g.setFont(new Font("Atlantic Inline", 3 - rand.nextInt(3), 20));
g.drawString(str, 60, 28 - rand.nextInt(13));
// 随机产生干扰线
// Random rand = new Random();
int lineWidth=2;
for (int i = 0; i < 250; i++) {
g.setColor(getColor());
int x1 = rand.nextInt(width - lineWidth - 1) + 1; // 保证画在边框之内
int y1 = rand.nextInt(height - lineWidth - 1) + 1;
int x2 = rand.nextInt(lineWidth);
int y2 = rand.nextInt(lineWidth);
g.drawLine(x1, y1, x1+x2, y1+y2);
// int x1 = rand.nextInt(80);
// int y1 = rand.nextInt(30);
// int x2 = rand.nextInt(80);
// int y2 = rand.nextInt(30);
// g.drawLine(x1, y1, x2, y2);
}
// 释放图形上下文
g.dispose();
try {
// 输出图形到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
} catch (IOException e) {
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
private Color getColor() {
Random random = new Random();
int red = random.nextInt(180);
int green = random.nextInt(180);
int blue = random.nextInt(180);
Color color = new Color(red, green, blue);
return color;
}
}
js代码:
function login(){
fnLogin();
}
function fnLogin()
{
var inputCode = document.getElementById("vcode").value.toLowerCase();
if(inputCode.length <=0)
{
alert("请输入验证码!");
$('#vcode').focus();
return ;
}
if(inputCode != code.toLowerCase())
{
alert("验证码输入错误!");
$('#vcode').focus();
show();//刷新验证码
return ;
}
if($('#loginname').val() == ''){
alert('登录名不能为空');
$('#loginname').focus();
return;
}
if($('#password').val() == ''){
alert('密码不能为空');
$('#password').focus();
return;
}
//$('#butLogin').linkbutton('disable');
//$('#butReset').linkbutton('disable');
document.getElementById("butLogin").disabled="disabled";
document.form1.submit();
}
var code ; //在全局 定义验证码
function createCode()
{
code = "";
var codeLength = 4;//验证码的长度
//所有候选组成验证码的字符,可以用中文
var selectChar = new Array(0,1,2,3,4,5,6,7,8,9,'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','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');
for(var i=0;i<codeLength;i++)
{
var charIndex = Math.floor(Math.random()*60);
code +=selectChar[charIndex];
}
return code;
}
function show(){
//显示验证码
var s=document.getElementById("code").src="codeServlet?code="+createCode();
}