创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

使用Session实现一个一次性的验证码登录功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
验证码的实现原理:
在一个Servlet中生成验证,并把验证码上的数据保存在Session,用户提交验证码之后,会提交给另外一个Servlet程序。在获取用户提交数据的Servlet中的从Session中把验证码取出,在取出的同时从Session中把验证码删除。
1.注册页面:register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" >
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
-->
<script type="text/javascript">
function _changImg(){
document.getElementById("myimg").src = "/day11/checkImg?" + new Date().getMilliseconds();
}
</script>
</head>
<body>
<center>
<form action="/day11/regist" method="post">
<table>
<tr>
<td>用户名<font color="red">*</font></td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>输入验证码</td>
<td>
<input type="text" name="form_checkcode" />
<img alt="" src="/day11/checkImg" id="myimg" onclick="_changImg();"/>
<a href="javascript:void(0);" rel="external nofollow" onclick="_changImg();">看不清,换一张</a>
<font color="red">${imgError }</font>
</td>
</tr>
<tr>
<td><input type="submit" value="注册" /></td>
</tr>
</table>
</form>
</center>
</body>
</html>