JSP +MySQL實現網站的登錄與註冊小案例
阿新 • • 發佈:2019-01-26
register 養成 sub man put gin print 鏈接 ces
為了練手,我就自己試著做了一個網站的登錄與註冊的小案例。由於沒有做美化處理,所以界面並不是很好看。
網站實現的功能如下:
- 用戶首次註冊功能
- 用戶登錄功能
項目目錄展示:
下面我將會分模塊展示
註冊模塊
首先需要一個註冊界面,如下register.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>User to Register Page!</title>
</head>
<body>
<hr><br>Welcome to this <font color="green">Enroll(Register) Page</font>!<br>
<form action="do_register.jsp" method="get">
<br>
<h1>Please input your message:</h1><br>
Name:<input type="text" name="register_name"><br>
Pswd:<input type="password" name="register_password"><br>
<br><br><br>
<input type="submit"> <input type="reset"><br>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
然後就是action對應的註冊處理頁,如下do_register.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Server to do the register page!</title>
</head>
<body>
<%
String Register_name=request.getParameter("register_name");
String Register_password=request.getParameter("register_password");
%>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/summer", "root", "mysql");
Statement stmt=conn.createStatement();
//desogn the sql statement
String InsertSQL="INSERT INTO User(Name,Password) values(‘"+Register_name+"‘,‘"+Register_password+"‘)";
System.out.println(Register_name+"\t"+Register_password);
//do the query operation,and here is the most important sql statement.
int FLAG=stmt.executeUpdate(InsertSQL);
if(FLAG>0){
response.getWriter().write("Congratulation! REgister Success!");
}else{
response.getWriter().write("Sorry!Register Failed!\nPlease Retry it!");
}
}catch(SQLException e){
}
%>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
小總結:
不足之處:
- 對於數據庫的操作做得不夠好,沒有及時的將不用的資源關閉,應該及時的對那些不用的打開的資源進行關閉操作,釋放資源。
- 界面效果做的不夠好,response輸出是先於out的輸出的。
- 數據庫操作顯得過於繁瑣,應該集成一下,做一個專門處理數據庫操作的工具包,以實現代碼的良好的復用性!
登錄模塊
首先是登錄界面,login.jsp,鄙人加進去一個超鏈接(用意是讓login.jsp作為門戶頁面,實現登錄註冊合二為一的效果,雖然二者並沒有合二為一,而且註冊界面過於簡單了),大家就先湊活看吧。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>User Login Page</title>
</head>
<body>
<hr><br>Welcome to this <font color="green">Login Page</font>!<br>
<form action="do_login.jsp" method="get">
<br>
<h1>Please input your message:</h1><br>
Name:<input type="text" name="name"><br>
Pswd:<input type="password" name="password"><br>
<br><br><br>
<input type="submit"> <input type="reset"><br>
Click me to <font color="green"><a href="register.jsp">Register</a>!</font><br>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
然後是對登錄信息的處理頁,do_login.jsp:
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Server Page Depend !</title>
</head>
<body>
<h3>Which Pae will be depend by the user‘s message!</h3>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
%>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/summer", "root", "mysql");
Statement stmt=conn.createStatement();
//desogn the sql statement
String queryNumberSQL="SELECT Name from User where Name=‘"+name+"‘ and Password=‘"+password+"‘";
//do the query operation
ResultSet rs=stmt.executeQuery(queryNumberSQL);
boolean flag=false;
if(rs.next()){
flag=true;
session.setAttribute("UserName", name);
}else{
flag=false;
}
%>
<%
if(flag){
%>
<jsp:forward page="login_success.jsp"></jsp:forward>
<%
}else{
%>
<jsp:forward page="login_failed.jsp"></jsp:forward>
<%
}
%>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
對於登陸成功的用戶,跳轉到登陸成功界面login_success.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>User Login Success Page!</title>
</head>
<body>
<hr><br>
<h1>Login Success!</h1><br>
<font color="green">Welcome <%=session.getAttribute("UserName") %>!</font>
<h3 align="center">your persional Message is:</h3>
<%
out.println("Name:"+session.getAttribute("UserName"));
%>
<font color="red"><a href="login.jsp">Click me</a> to log out!</font>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
對於登錄失敗的用戶,進行溫馨的頁面提示,login.failed.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Login Failed Page!</title>
</head>
<body>
<hr>
<br>
<h1><font color="red">Sorry,Login Failed</font></h1><br>
<font color="red"><a href="login.jsp">Click me</a> to login!</font>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
大總結:
進步之處:
- 使用到了session對象來存儲用戶登錄的姓名信息,實現了頁面間的信息的交互
- 配合了MySQL,在一定程度上體驗了JEE的模式
不足之處:
- 代碼過於繁冗,復用性不好
- 資源利用率不高,使用過的不再使用的資源要及時的進行關閉。雖然java虛擬機有自動的垃圾回收機制,但最好還是養成好的習慣!
- 界面控制做的不夠好,體驗性差,欠缺思考
待改進之處:
- 加上復雜一點的用戶註冊,使用bean的方式做處理比較好
- 模塊化,使用MVC的概念
- 改善界面的權限,防止盜鏈
- 加上其他的諸如上傳文件,下載文件功能,豐富網站的功能。
再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow
JSP +MySQL實現網站的登錄與註冊小案例