烈火建站學院(Bkjia.Com)JSP文檔 前台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"
onfocus="if(this.value=='Your name')this.value='';"><br>密碼:
<input type="password" name="txtPassword" value="Your password"
onfocus="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>