All Rights Reserved!
前臺login.html和後臺verifylogin.jsp兩個頁面組成:
login.html內容:
<html>
<head>
<title>登錄</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="ch-cn">
</head>
<body>
<!-- Form 用來提取用戶填入並提交的信息-->
<form method="post" name="frmLogin" action="verifylogin.jsp">
<h1 align="center">用戶登錄</h1><br>
<div align="center">用戶名:
<input type="text" name="txtUserName" value="Your name"
οnfοcus="if(this.value=='Your name')this.value='';"><br>密碼:
<input type="password" name="txtPassword" value="Your password"
οnfοcus="if(this.value=='Your password')this.value='';"><br>
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Reset" value="重置"><br>
</div></form></body>
</html>
verifylogin.jsp內容:
<%@ page language="java" contentType="text/html;charset=gb2312"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登錄</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">
-->
</head>
<body>
<div align=center>
<%
//獲取用戶名
String sUserName = request.getParameter ( "txtUserName" );
//獲取密碼
String sPasswd = request.getParameter ( "txtPassword" );
//登記JDBC驅動程序
Class.forName ( "org.gjt.mm.mysql.Driver" ).newInstance ( );
//連接參數與Access不同
String url = "jdbc:mysql://localhost/LearnJSP";
//建立連接
Connection connection = DriverManager.getConnection ( url, "root",
"011124" );
//SQL語句
String sql = "select * from userinfo where username='" + sUserName
+ "' and userpwd = '" + sPasswd + "'";
Statement stmt = connection.createStatement ( );
ResultSet rs = stmt.executeQuery ( sql ); //返回查詢結果
//如果記錄集非空,表明有匹配的用戶名和密碼,登陸成功
if ( rs.next ( ) )
{
out.println ( "登錄成功!" );
} else
//否則登錄失敗
{
out.println ( "用戶名不存在或密碼錯誤!" );
}
rs.close ( );
stmt.close ( );
connection.close ( );
%>
</body>
</html>
下面爲客戶端添加代碼驗證功能:
<html>
<head>
<title>登錄</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="ch-cn">
</head>
<body>
<!-- Form 用來提取用戶填入並提交的信息-->
<form method="post" name="frmLogin" action="verifylogin.jsp">
<h1 align="center">用戶登錄</h1><br>
<div align="center">用戶名:
<input type="text" name="txtUserName" value="Your name"
οnfοcus="if(this.value=='Your name')this.value='';"><br>密碼:
<input type="password" name="txtPassword" value="Your password"
οnfοcus="if(this.value=='Your password')this.value='';"><br>
<input type="submit" name="Submit" value="提交" onClick="validateLogin();" >
<input type="reset" name="Reset" value="重置"><br>
</div>
</form>
<!-- javaScript 函數 validateLogin(),用來驗證用戶名和密碼是否爲空 -->
<script language="javaScript">
function validateLogin()
{
var sUserName = document.frmLogin.txtUserName.value;
var sPassword = document.frmLogin.txtPassword.value;
if( sUserName=="" )
{
alert("請輸入用戶名!");
return false;
}
if( sPassword=="" )
{
alert("請輸入密碼!");
return false;
}
}
</script>
</body>
</html>
爲服務器端添加代碼驗證功能:
<%@ page language="java" contentType="text/html;charset=gb2312"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登錄</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="zieckey,jsp">
<meta http-equiv="description" content="Test JSP using MySQL">
</head>
<body>
<div align=center>
<%
//獲取用戶名
String sUserName = request.getParameter ( "txtUserName" );
if ( sUserName == "" || sUserName == null || sUserName.length()>20 )
{
try
{
response.sendRedirect ( "login.html" );
} catch ( Exception e )
{
}
}
//獲取密碼
String sPasswd = request.getParameter ( "txtPassword" );
if ( sPasswd == "" || sPasswd == null || sPasswd.length()>20 )
{
try
{
response.sendRedirect ( "login.html" );
} catch ( Exception e )
{
}
}
//登記JDBC驅動程序
Class.forName ( "org.gjt.mm.mysql.Driver" ).newInstance ( );
//連接參數與Access不同
String url = "jdbc:mysql://localhost/LearnJSP";
//建立連接
Connection connection = DriverManager.getConnection ( url, "root",
"011124" );
//SQL語句
String sql = "select * from userinfo where username='" + sUserName
+ "' and userpwd = '" + sPasswd + "'";
Statement stmt = connection.createStatement ( );
ResultSet rs = stmt.executeQuery ( sql ); //返回查詢結果
//如果記錄集非空,表明有匹配的用戶名和密碼,登陸成功
if ( rs.next ( ) )
{
//登錄成功後將sUserName設置爲session變量的UserName
//這樣在後面就可以通過 session.getAttribute("UserName") 來獲取用戶名,
//同時這樣還可以作爲用戶登錄與否的判斷依據
session.setAttribute ( "UserName", sUserName );
out.print ( "登錄成功!" );
out.print ( session.getAttribute ( "UserName" ) + " 歡迎您!" );
} else
//否則登錄失敗
{
out.println ( "用戶名不存在或密碼錯誤!" );
}
rs.close ( );
stmt.close ( );
connection.close ( );
%>
</body>
</html>
數據庫中所有表的字段長度的設計標準是應該是足夠用,但不浪費存儲空間.
我們可以發現,上面數據庫中字段限制在20個字符以內,那麼程序中也應該作一個限制,
否則可能給網站出現嚴重的問題.
將上面源碼修改如下:
.....
<input type="text" name="txtUserName" value="Your name"
size="20" maxlength="20"
οnfοcus="if(this.value=='Your name')this.value='';"><br>密碼:
<input type="password" name="txtPassword" value="Your password"
size="20" maxlength="20"
οnfοcus="if(this.value=='Your password')this.value='';"><br>
.....
.....
if ( sUserName == "" || sUserName == null || sUserName.length()>20 )
....
if ( sPasswd == "" || sPasswd == null || sPasswd.length()>20 )
......
這樣問題就徹底解決了.