📄 registok.jsp
字号:
<%-- 这个JSP脚本是判断用户注册信息的正确性 --%>
<%@ page language="java" %> <%-- 脚本所用语言 --%>
<%@ page contentType="text/html;charset=gb2312" %> <%-- 定义JSP页面的字符编码和对应的MIME类型 --%>
<%@ page errorPage="errorpage.jsp" %> <%-- 定义捕捉例外的对应脚本,当有例外时,自动跳转到此页面 --%>
<%@ page import="java.sql.*" %> <%-- 装载Java的特定类库 --%>
<%@ page import="java.io.*" %>
<html>
<head>
<title>利用Cookie实现身份验证</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.tableline { border-color: #CE00CE #ce00ce #ce00ce; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
.buttonline { background-color: #FFFFFF; border-color: #8080FF #8080ff #8080ff; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
.tableline2 { border-color: #FF6633 #ff6633 #ff6633; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
-->
</style>
</head>
<%
Connection con;
Statement stmt;
ResultSet rset;
String getTxt_Name = null;
String getTxt_Password = null;
String getTxt_Password2 = null;
String getTxt_email = null;
String getTxt_RandomCode = null;
String getTxtTemp = null;
String getSessionTxt = null;
String sSQLCmd = null;
int flag = 0;
/* 判断提交的信息是否完整 */
getTxtTemp = request.getParameter("textfield_name"); // 获取用户名
if((getTxtTemp == null) || (getTxtTemp.length() < 1))
{
throw new Exception("用户名不可以为空!<br>请点击“后退”按钮重新填写注册表单。");
}
else
{
getTxt_Name = new String(getTxtTemp.getBytes("iso-8859-1"));
getTxt_Name = getTxt_Name.trim();
getTxt_Password = request.getParameter("textfield_password1"); // 获取密码
getTxt_Password2 = request.getParameter("textfield_password2"); // 获取确认密码
if((getTxt_Password == null) || (getTxt_Password.length() < 1))
{
throw new Exception("密码不可以为空!<br>请点击“后退”按钮重新填写注册表单。");
}
else
{
if(!(getTxt_Password.equals(getTxt_Password2)))
{
throw new Exception("所填的密码与确认值不一致!<br>请点击“后退”按钮重新填写注册表单。");
}
else
{
getTxt_email = request.getParameter("textfield_email"); // 获取Email
if((getTxt_email == null) || (getTxt_email.length() < 1))
{
throw new Exception("Email地址不可以为空!<br>请点击“后退”按钮重新填写注册表单。");
}
else
{
getTxt_RandomCode = request.getParameter("textfield_random"); // 获取随机ID码
getSessionTxt = (String)session.getValue("originalRandomID"); // 从session中取出服务器生成的随机ID码
getSessionTxt = getSessionTxt.trim();
if(getSessionTxt.equals(getTxt_RandomCode))
{
session.removeValue("originalRandomID"); // 删除原来的session值,以免影响到下一次的判断
/* 接下来进行数据库操作 */
/* 首先验证用户名的唯一性,然后将用户信息加入到数据库中 */
/* 当所有的验证都通过后,将用户名存入session中 */
sSQLCmd = "Select * from Member where Usename='" + getTxtTemp.trim() + "'";
try
{
/* 利用JDBC-ODBC桥建立数据库连接 */
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
con = DriverManager.getConnection("jdbc:odbc:DemoDB","demo","demo");
stmt = con.createStatement ();
rset = stmt.executeQuery (sSQLCmd);
while(rset.next())
{
flag = 1;
}
if(flag == 1)
{
throw new Exception("你所注册的用户名已经存在,请换一个用户名重新注册!");
}
else
{
/* 将用户信息插入数据库中,并且写入Cookie */
sSQLCmd = "insert into Member(Usename,Password,RandomID,Email) values('" + getTxt_Name + "','" + getTxt_Password + "','" + getTxt_RandomCode + "','" + getTxt_email + "')";
stmt.executeUpdate(sSQLCmd);
session.putValue("originalusername",getTxt_Name);
/* 分别定义两个Cookie对象,并且将用户名和随机ID码存入Cookie中 */
Cookie namecookie = new Cookie("username",getTxt_Name);
Cookie randomid = new Cookie("randomid",getTxt_RandomCode);
Cookie hadlogin = new Cookie("hadlogin","1"); // 定义一个hadlogin的Cookie对象,此Cookie对象用来检测用户是否已经成功登录
hadlogin.setMaxAge(8640000); // 定义Cookie的有效期限
namecookie.setMaxAge(8640000);
randomid.setMaxAge(8640000);
response.addCookie(namecookie); // 将所定义的Cookie对象发送到客户端
response.addCookie(randomid);
response.addCookie(hadlogin);
}
rset.close();
con.close();
}
catch(SQLException e)
{
throw new SQLException(e.toString());
}
}
else
{
throw new Exception("提交的随机ID号与服务器产生的ID号不一致!<br>请点击“后退”按钮重新填写注册表单。");
}
}
}
}
}
%>
<body bgcolor="#FFFFFF" link="#009900" vlink="#009900" alink="#009900">
<table width="100%" border="0" class=tableline>
<tr>
<td height="257">
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td> </td>
</tr>
<tr>
<td>
<div align="center"><font face="方正姚体" size="+3" color="#7777BB"><font face="方正舒体" size="+5">梦幻</font>网会员注册信息</font></div>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr align="center">
<td height="2">
<table width="80%" border="0" cellspacing="0" cellpadding="0" class="tableline2" bgcolor="#FFE1E1">
<tr>
<td width="47%">
<div align="right"></div>
</td>
<td width="53%"> </td>
</tr>
<tr>
<td width="47%">
<div align="right"><font face="幼圆">会员姓名:</font></div>
</td>
<td width="53%"><font face="幼圆"><%=getTxt_Name%></font></td>
</tr>
<tr>
<td width="47%">
<div align="right"><font face="幼圆">密 码:</font></div>
</td>
<td width="53%"><font face="幼圆"><%=getTxt_Password%></font></td>
</tr>
<tr>
<td width="47%">
<div align="right"><font face="幼圆">电子邮件:</font></div>
</td>
<td width="53%"><font face="幼圆"><%=getTxt_email%></font></td>
</tr>
<tr>
<td width="47%">
<div align="right"><font face="幼圆">随机ID码:</font></div>
</td>
<td width="53%"><font face="幼圆"><%=getTxt_RandomCode%></font></td>
</tr>
<tr>
<td width="47%" height="2"><font face="幼圆"></font></td>
<td width="53%" height="6"><font face="幼圆"></font></td>
</tr>
<tr>
<td width="47%" height="2"> </td>
<td width="53%" height="6"> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="2">
<div align="center"></div>
</td>
</tr>
<tr>
<td height="8">
<div align="left"><font face="方正姚体" size="5" color="#3333FF"> <%=(String)session.getValue("originalusername")%>会员:</font></div>
</td>
</tr>
<tr>
<td height="8">
<div align="center"><font face="方正姚体" size="5" color="#3333FF">祝贺您,您已经成功通过了会员身份的注册!</font></div>
</td>
</tr>
<tr>
<td height="2"><font color="#3333FF" face="方正姚体" size="5">请点击<a href="http://127.0.0.1/chapter5/poll.jsp"><i>此处</i></a>进入投票页面。</font></td>
</tr>
<tr>
<td height="2"> </td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -