[置頂] JSP實現登錄注冊並鏈接數據庫頁面



        在學習了頁面跳轉及部分知識后做了登錄注冊界面,並經過本博主調試bug后完善的更進一步,大家有什么問題也可以留言,本博主以更廣泛學習討論為目的。

        內容介紹:實現頁面的跳轉;注冊登錄時實現讀取數據庫,並對數據庫實現插入(insert)和查詢(select)功能。

        幾點注意:sqljdbc.jar包的導入和環境變量;數據庫的登錄讀取,可以參考數據庫的測試。

登錄注冊界面的代碼實現

index.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%>">        <title>My JSP 'Feilong_index.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">-->  </head>  <body>    <center>    <font face = "宋體" size = "6" color = "#000">歡迎使用飛龍科技</font><hr>    <div>        <img alt="" width = "600" height = "400" src="D:\我的圖片\QImages\小人團隊.jpg">    </div>    <table width = "200" border ="1" bordercolor = "#00F">        <tr>          <td><input type = "button" value = "登      陸" onclick = "window.location.href('login.jsp')"></td>          <td><input type = "button" value = "注      冊" onclick = "window.open('register.jsp')"></td>        </tr>     </table>  </center>  </body></html>

login.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%>">

<title>My JSP 'Feilong_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">
-->

<body>
<center>
<font face="楷體" size="6" color="#000" >登錄界面</font>
<%
String flag = request.getParameter("errNo");
try{
if(flag!=null)
out.println("用戶名不存在或密碼錯誤");
}catch(Exception e){
e.printStackTrace();
}
%>
<form action = "loginCh.jsp" method="post">
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>賬 戶:</th>
<td><input type="text" name="name" value = "請輸入用戶名" maxlength = "16" onfocus = "if(this.value == '請輸入用戶名') this.value =''"></td>
</tr>
<tr>
<th>密 碼:</th>
<td><input type="password" name="pwd" maxlength = "20"></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type="submit" name="submit" value="登 錄">
<input type="button" value="返 回"
onclick="window.location.href('/webText')">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
loginCh.jsp  //登錄檢驗
<%@ page language="java" import="java.util.*,java.sql.*,java.net.*" 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%>">

<title>My JSP 'Feilong_loginCh.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">
-->

</head>
<body>
<% //接收用戶名和密碼
String user = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("pwd");

String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433; DatabaseName = db_01";
String username = "sa";
String password = "123";
Class.forName(driverClass);//加載驅動
Connection conn = DriverManager.getConnection(url,username,password);//得到連接
PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "' and Pwd = '" + pwd + "'");
ResultSet rs = pStmt.executeQuery();
if(rs.next()){
response.sendRedirect("success.jsp?username="+URLEncoder.encode(user)); //解決亂碼
}else{
response.sendRedirect("login.jsp?errNo");//密碼不對返回到登陸
}
rs.close();
pStmt.close();
conn.close();
%>
</body>
</html>
success.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%>">
<title>Feilong_登錄成功</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>
<center>
<%
String name = new String(request.getParameter("username").getBytes("8859_1"));
out.println("歡迎你:" + name);
%><br>
<a href="login.jsp">重新登陸</a>
</center>
</body>
</html>

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%>">
<title>My JSP 'Feilong_register.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">
-->
<script>
function addCheck(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var newword=document.getElementById("newword").value;
if(username==""){
alert("用戶名不能為空!");
document.getElementById("username").focus();
return false;
}
if(password==""){
alert("密碼不能為空!");
document.getElementById("password").focus();
return false;
}
if(password != newword){
alert("兩次輸入密碼不相同!");
document.getElementById("newword").focus();
return false;
}
}
function validate(){
var flag = addCheck();
if(flag == false)
return false;
return true;
}
</script>
<body>
<center>
<font face="楷體" size="6" color="#000">注冊界面</font>
<form action = "checkRegister.jsp" method = "post" onsubmit = "return validate()">
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>用戶名:</th>
<td><input type="text" name="username" value="輸入16個字符以內" maxlength = "16" onfocus = "if(this.value == '輸入16個字符以內') this.value =''"></td>
</tr>
<tr>
<th>輸入密碼:</th>
<td><input type="text" name="password" value="輸入20個字符以內" maxlength = "20" onfocus = "if(this.value == '輸入20個字符以內') this.value =''"></td>
</tr>
<tr>
<th>確認密碼:</th>
<td><input type="text" name="newword" value="重新輸入密碼" maxlength = "20" onfocus = "if(this.value == '重新輸入密碼') this.value =''"></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type="submit" value="注 冊">    
<input type="reset" value="重 置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>

checkRegister.jsp //驗證注冊用戶並導入數據庫

<%@ page language="java" import="java.util.*,java.sql.*" 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%>">

<title>My JSP 'Feilong_chechRegister.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">
-->

</head>
<body>
<%
String user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("password");

String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433; DatabaseName = db_01";
String username = "sa";
String password = "123";
Class.forName(driverClass);//加載驅動
Connection conn = DriverManager.getConnection(url,username,password);//得到連接
PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'");
ResultSet rs = pStmt.executeQuery();
if(rs.next()){
out.println("<script language='javascript'>alert('該用戶已存在,請重新注冊!');window.location.href='register.jsp';</script>");
}else{
PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')");
int rst = tmt.executeUpdate();
if (rst != 0){
out.println("<script language='javascript'>alert('用戶注冊成功!');window.location.href='index.jsp';</script>");
}else{
out.println("<script language='javascript'>alert('用戶注冊失敗!');window.location.href='register.jsp';</script>");
}
}
%>
</body>
</html>

以上代碼若有問題,首先執行下面數據庫的測試代碼:

執行下面代碼前,首先在數據庫中創建名為‘db_01’的數據庫,在表中創建名為‘user’的表

login1.jsp

<%@ page language="java" import="java.util.*,java.sql.*" 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%>">

<title>My JSP 'Feilong_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">
-->

</head>
<body>
<%
String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433; DatabaseName = db_01";
String username = "sa";
String password = "123";
Class.forName(driverClass);//這步錯可能是因為.jar包導入問題
Connection conn = DriverManager.getConnection(url,username,password);//這步錯可能是因為數據庫屬性安全中的名、密碼不對或SQL的IP端口不是‘1433’
PreparedStatement pStmt = conn.prepareStatement("select * from tb_user");
ResultSet rs = pStmt.executeQuery();
while(rs.next()){
out.println("用戶名: " + rs.getString(1)
+ " 密碼: " + rs.getString(2));
}
rs.close();
pStmt.close();
conn.close();
%>
</body>
</html>

小人團隊.jpg

以上代碼都是經過本博主細心敲打修改並實現的。轉載請注明本博客的地址。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: