📄 loginok.jsp
字号:
<%-- 这个JSP脚本是判断用户登录信息的正确性 --%>
<%@ page language="java" %> <%-- 脚本所用语言 --%>
<%@ page contentType="text/html;charset=gb2312" %> <%-- 定义JSP页面的字符编码和对应的MIME类型 --%>
<%@ page errorPage="errorpage.jsp" %> <%-- 定义捕捉例外的对应脚本,当有例外时,自动跳转到此页面 --%>
<%@ page import="java.sql.*" %> <%-- 装载Java的特定类库 --%>
<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">
<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>
<meta http-equiv="refresh" content="10;URL=http://127.0.0.1/chapter5/poll.jsp">
</head>
<body bgcolor="#FFFFFF" link="#009900" vlink="#009900" alink="#009900">
<table width="100%" border="0" class=tableline>
<tr>
<td height="97">
<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>
<%
Connection con;
Statement stmt;
ResultSet rset;
String getTxtTemp = null;
String getTxt_Name = null;
String getTxt_Password = null;
String getTxt_RandomCode = 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"); // 获取密码
if((getTxt_Password == null) || (getTxt_Password.length() < 1))
{
throw new Exception("密码不可以为空!<br>请点击“后退”按钮重新填写登录表单。");
}
else
{
getTxt_RandomCode = request.getParameter("textfield_random"); // 获取随机ID码
if((getTxt_RandomCode == null) || (getTxt_RandomCode.length() < 1))
{
throw new Exception("随机ID码不可以为空!<br>请点击“后退”按钮重新填写登录表单。");
}
else
{
/* 接下来进行数据库操作,判断用户是否和数据库中注册的信息相同 */
sSQLCmd = "Select * from Member where Usename='" + getTxt_Name.trim() + "' and Password='" + getTxt_Password + "' and RandomID = '" + getTxt_RandomCode.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;
}
rset.close();
con.close();
if(flag != 1)
{
throw new Exception("登录信息不正确,请重新登录!");
}
else
{
/* 将用户信息插入数据库中,并且写入Cookie */
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);
}
}
catch(SQLException e)
{
throw new SQLException(e.toString());
}
}
}
}
%>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td height="8">
<%
String memberName = null;
memberName = (String)session.getValue("originalusername");
if((memberName == null) || (memberName.length() < 1))
{
throw new Exception("访问者的身份不正确,请重新登录!");
}
%>
<div align="left"><font face="方正姚体" size="5" color="#3333FF"> <%=memberName%>会员:</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 + -