首页 > 代码库 > 验证码代码编写
验证码代码编写
index.jsp 页面
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html:html>
<head>
<title><bean:message key="application.title" /></title>
<link rel="icon" href="http://www.mamicode.com/images/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="http://www.mamicode.com/styles/login.css" type="text/css"
media="all" />
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta name="author" content="neusoft" />
<meta name="description" content="MCIS System" />
<meta name="copyright" content="copyright (c) 2008 neusoft" />
<style type="text/css">
<!--
.style1 { border-color: #009900 }
-->
</style>
</head>
<body onl oad="load();">
<div id="layout-left"></div>
<div id="layout-middle">
<div id="body">
<div id="body_left">
<p></p>
</div>
<div id="body_right">
<html:form method="post" action="/logon.do" focus="user.value(ID)"
onsubmit="return clickSubmit();">
<ul>
<li></li>
<li>
用户名:
<br>
<html:text size="16" property="user.value(ID)"
onkeyup="checkAdmin();" onblur="checkAdmin();"
style="width:160;height:20" maxlength="16"/>
</li>
<li>
密  码:
<br>
<!--<input class="fixed" type="password" size="16" id="password"/>-->
<html:password size="16" property="user.value(PASSWORD)"
style="width:160;height:20" maxlength="10"/>
</li>
<li>
验证码:
<br>
<html:text property="user.value(rs)" styleId="rs"
maxlength="4" style="width:100;height:20" value=""
onfocus="if(this.value=http://www.mamicode.com/=‘‘) this.value=‘‘;"
onblur="if(this.value=http://www.mamicode.com/=‘‘) {this.value=‘‘;}" />
<a href="javascript:show(document.getElementById(‘hcode‘))"><img
id="hcode" title="点击刷新"
src="http://www.mamicode.com/random.jsp" hspace="15" class="style1"
style="text-decoration:none" /></a>
</li>
<li>
登录方式:
<html:checkbox styleId="adminType" style="border: none"
property="user.value(TYPE)" value="http://www.mamicode.com/0" />
独占模式
</li>
<li>
<input class="bt" type="submit" value="http://www.mamicode.com/登 录" />
<!--<a href="http://www.mamicode.com/#">忘记密码?</a>-->
<input class="bt" type="button" value="http://www.mamicode.com/重 置"
onclick="clickReset();">
</li>
</ul>
</html:form>
<div id="message">
<%@ include file="../common/messagehandle.jsp"%>
</div>
<span id="logo"></span>
</div>
</div>
</div>
<div id="layout-right"></div>
<script type="text/javascript">
<!--
var windowName = "processWindow";
function init(){
setTitle2Top();
window.name = windowName;
}
function onEnter(){
if(window.event.keyCode == 13) //if press the Enter key
//window.event.keyCode = 9; //replace it with the Tab key
userBean.submit();
}
function checkAdmin(){
if(‘admin‘== userBean.elements["user.value(ID)"].value){
document.getElementById("adminType").disabled=false;
}else{
document.getElementById("adminType").disabled="true";
}
}
function clickSubmit(){
var main = document.getElementById("main");
var errMsg = document.getElementById("message");
var Id=userBean.elements["user.value(ID)"].value;
var orignPass=userBean.elements["user.value(PASSWORD)"].value;
var RS=userBean.elements["user.value(rs)"].value;
var orignRS=userBean.elements["user.value(rs)"].value;
if(Id == null || Id==""){
errMsg.style.color="#CC0000";
errMsg.innerText = "请输入用户名";
userBean.elements["user.value(ID)"].focus();
return false;
}
if(orignPass == null || orignPass==""){
errMsg.style.color="#CC0000";
errMsg.innerText = "请输入密码";
userBean.elements["user.value(PASSWORD)"].focus();
return false;
}
if(orignRS == null || orignRS==""){
errMsg.style.color="#CC0000";
errMsg.innerText = "请输入验证码";
userBean.elements["user.value(rs)"].focus();
return false;
}
else {
return true;
//userBean.submit();
}
}
function clickReset(){
userBean.reset();
load();
var errMsg = document.getElementById("message");
errMsg.innerText="";
}
function load(){
//document.getElementById("type1").checked="true";
//alert(document.getElementById("type2").disabled);
document.getElementById("adminType").disabled=true;
}
function show(o){//重载验证码
var timenow = new Date().getTime();
o.src="http://www.mamicode.com/random.jsp?d="+timenow;
//超时执行;
setTimeout(function(){
o.src="http://www.mamicode.com/random.jsp?d="+timenow;
}
);
}
//-->
</script>
</body>
<html:javascript formName="userBean" dynamicJavascript="true" staticJavascript="true"/>
</html:html>
random.jsp
<%@ page contentType="image/jpeg; charset=GBK"%>
<%@ page import="java.awt.*,java.awt.image.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.sun.image.codec.jpeg.*"%>
<%!//产生随机颜色函数getRandColor
Color getRandColor(int fc, int bc) {
Random r = new Random();
if (fc > 255) fc = 255;
if (bc > 255) bc = 255;
int red = fc + r.nextInt(bc - fc); //red
int green = fc + r.nextInt(bc - fc); //green
int blue = fc + r.nextInt(bc - fc); //blue
return new Color(red, green, blue);
}%>
<%
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//创建随机类
Random r = new Random();
//在内存中创建图像,宽度为width,高度为height
int width = 60, height = 20;
BufferedImage pic = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取图形上下文环境
Graphics gc = pic.getGraphics();
//设定背景图形上下文填充
gc.setColor(getRandColor(200, 250));
gc.fillRect(0, 0, width, height);
//设定图形上下文环境字体
gc.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//随机产生50条干扰直线,使图像中的认证码不易被其他分析程序探测
gc.setColor(getRandColor(160, 200));
for (int i = 0; i < 50; i++) {
int x1 = r.nextInt(width);
int y1 = r.nextInt(height);
int x2 = r.nextInt(15);
int y2 = r.nextInt(15);
gc.drawLine(x1, y1, x1 + x2, y1 + y2);
}
//随机产生50个干扰点,使图像中的验证码不易被其他分析程序探测到
gc.setColor(getRandColor(120, 240));
for (int i = 0; i < 50; i++) {
int x = r.nextInt(width);
int y = r.nextInt(height);
gc.drawOval(x, y, 0, 0);
}
//随机产生4位数字的验证码
String RS = "";
String rn = "";
for (int i = 0; i < 4; i++) {
//产生10以内的随机数字rn
rn = String.valueOf(r.nextInt(10));
RS += rn;
//将认证码用drawString函数显示到图像里
gc.setColor(new Color(20 + r.nextInt(110), 20 + r.nextInt(110), 20 + r
.nextInt(110)));
gc.drawString(rn, 13 * i + 6, 16);
}
//释放图形上下文环境
gc.dispose();
//将认证码RS存入SESSION中共享
session.setAttribute("random", RS);
//输出生成后的验证码图像到页面
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(pic);
out.clear();
out = pageContext.pushBody();
%>
验证码代码编写