<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>提交表单时加入验证码</title>
</head>
<body>
<form name="form1" action="check3" method="post"> <!-- 定义form表单 -->
<table width="300" height="160" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="40" valign="bottom">用户名: <input name="USERNAME" type="text" class="input2"
onKeyDown="if(event.keyCode==13){form1.PWD.focus();}"
onMouseOver="this.style.background='#F0DAF3';"
onMouseOut="this.style.background='#FFFFFF'"/>
</td> <!-- 设置用户名文本框,并设置了鼠标经过时的样式 -->
</tr>
<tr>
<td height="40" colspan="2" valign="bottom" class="STYLE15">密 码:
<input name="PWD" type="password" class="inout2" align="bottom"
onKeyDown="if(event.ketCode==13){form1.yanzhengfocus();}"
onMouseOver="this.style.background='#F0DAF3';"
onMouseOut="this.style.background='#FFFFFF';"/> <!-- 设置密码我文本框 -->
</td>
</tr>
<tr>
<td height="40" colspan="2" valign="top" class="STYLE15"
οndragstart="return flase" οnselect="return flase">验证码:
<input name="yanzheng" type="text" class="input2"
onkeyDown="if(event.keyCode==13){form1.submit.focus();}"
size="8" align="bottom"
onMouseOver="this.style.background='#F0DAF3';"
onMouseOut="this.style.background='#FFFFFF';"/> <!-- 设置验证码验证框 -->
<%
int intmethod1=(int)((Math.random()*10)-1);
int intmethod2=(int)((Math.random()*10)-1);
int intmethod3=(int)((Math.random()*10)-1);
int intmethod4=(int)((Math.random()*10)-1);
String intsum=intmethod1+""+intmethod2+intmethod3+intmethod4;
%>
<input type="hidden" name="verifycode" value="<%=intsum%>"/> <!-- 设置隐藏域,用来验证比较 -->
<span class="STYLE15"><font size="+3" color="#FF0000">
<img src="num/<%=intmethod1%>.jpg"> <!--将图片的文件夹放在webContent文件夹下 -->
<img src="num/<%=intmethod2%>.jpg">
<img src="num/<%=intmethod3%>.jpg">
<img src="num\<%=intmethod4%>.jpg"></font></span>
</td>
</tr>
<tr>
<td height="40" colspan="2" valign="bottom" align="center">
<input name="submit" type="button" class="submit1" value="登录" onClick="mycheck"/>
<input name="submit2" type="reset" class="submit1" value="重置"/>
</td> <!-- 设置提交于重置按钮 -->
</tr>
</table>
</form>
</body>
</html>
其中把数字图片裁剪为20px 放在webContent下的新建num文件夹下
jsp中添加验证码
最新推荐文章于 2021-06-01 03:36:43 发布