jsp實現登入註冊(與資料庫對接)
阿新 • • 發佈:2019-01-10
最近做了一些影象處理的內容,閒暇時間搞了下jsp,終於把至少兩個月之前的程式碼的bug找出來了...
具體內容我在之前一篇博文有介紹,主要是增加了資料庫的部分。其實一樣處理,獲得輸入的使用者名稱,密碼,然後判斷是否需要在當前頁面用javascipt處理下(比如註冊肯定是需要的,起碼兩次密碼輸的要一樣),然後跳轉邏輯頁面,對接資料庫,進行增刪查改,最後跳轉相應的頁面.
有幾點需要注意:1,myeclipse的匯入jar的包與eclipse稍有不同.
2,javascript處理和跳轉邏輯頁面的處理的區別
之後有空再用javabean重寫一遍.
login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%> <html> <head> <title> 使用者登入 </title> </head> <body bgcolor="#e3e3e3"> <center> <form action="check.jsp" method="post"> <table> <caption>使用者登入</caption> <tr><td>使用者名稱:</td><td><input type="text" name="username" size="20"/></td></tr> <tr><td>密碼:</td><td><input type="text" name="pwd" size="20"/></td></tr> <tr><td><input type="submit" value="登入"/><td><input type="reset" value="重置"/> </table> </form> 如果您還沒有註冊,請單擊<a href="register.jsp">這裡</a>註冊! </body> </center> </html>
register.jsp
check.jsp(判斷使用者登入)<%@ page language="java" import="java.util.*" contentType="text/html;charset=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>註冊頁面</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 language="javascript"> function isValid(form) { if (form.username.value=="") { alert("使用者名稱不能為空"); return false; } if (form.pwd.value!=form.pwd2.value) { alert("兩次輸入的密碼不同!"); return false; } else if (form.pwd.value=="") { alert("使用者密碼不能為空!"); return false; } else return true; } </script> </head> <body> <center> <body bgcolor="#e3e3e3"> <h2>使用者註冊</h2> <form action="check2.jsp" method="post" onSubmit="return isValid(this);"> <table> <tr><td>使用者名稱:</td><td><input type="text" name="username" size="20"/></td></tr> <tr><td>輸入密碼:</td><td><input type="text" name="pwd" size="20"/></td></tr> <tr><td>再次確認密碼:</td><td><input type="text"name="pwd2" size="20"/></td><tr> <tr><td><input type="submit" value="註冊"/><td><input type="reset" value="重置"/> </table> </form> </center> <br> </body> </html>
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=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 'check.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>
<%
request.setCharacterEncoding("utf-8");
String users=request.getParameter("username");
String pass=request.getParameter("pwd");
boolean flag=false;
PreparedStatement sql=null;
ResultSet rs=null;
Connection conn=null;
%>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3307/login";
String use = "root";
String password = "960404";
Class.forName(driver);
conn= DriverManager.getConnection(url,use,password);
sql =conn.prepareStatement("select * from student where username=? and password=?");
sql.setString(1,users);
sql.setString(2,pass);
rs=sql.executeQuery();
if (rs.next()) {
flag=true;
}
rs.close();
sql.close();
conn.close();
%>
<!-- 判斷是否是正確的登入使用者 -->
<% if (flag==true){ %>
<jsp:forward page="show.jsp"/>
<%}
else
if (flag==false){
%>
<jsp:forward page="login_fali.jsp"/>
<%} %>
</body>
</html>
check2.jsp(判斷使用者註冊)
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=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>檢驗註冊頁面</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>
<br>
<%
request.setCharacterEncoding("utf-8");
String users=request.getParameter("username");
String pass=request.getParameter("pwd");
%>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3307/login";
String use = "root";
String password = "960404";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url,use,password);
PreparedStatement sql =conn.prepareStatement("insert into student(username,password)values(?,?)");
sql.setString(1,users);
sql.setString(2,pass);
int rtn=sql.executeUpdate();
sql.close();
conn.close();
%>
</body>
</html>
show.jsp(登入成功)<%@ page language="java" import="java.util.*" contentType="text/html;charset=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>登入成功</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>
登入成功. <br>
</body>
</html>
login_fail.jsp(注意還是要跳轉回原來的登入頁面的)
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=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>登入失敗</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>
<% out.println("登入失敗");%>
<% response.setHeader("refresh","5;url=login.jsp");%>
</body>
</html>