使用MVC實現使用者登入註冊系統—銀行管理系統
一.介紹:使用MVC架構,設計一個銀行賬戶餘額顯示程式
二.功能:實現了登入和註冊功能,根據使用者帳戶餘額的不同,跳到不同的頁面顯示不同的資訊:
負的餘額:警示資訊
正常餘額:標準資訊
高餘額: 包括廣告
未知的客戶ID:錯誤頁面
三:用到的知識:javabean,html,jsp,servlet,css,javascript,伺服器Tomcat,已經MVC設計思想。
(註冊功能沒有實現,讀者可以自己試下哦,和登入的servlet差不多,就是向資料庫中新增一個你要註冊的使用者而已。)
四 實驗步驟
程式結構,如下圖,採用MVC模式進行程式設計
1. 建立一個使用者資料庫,如下圖
2. 登入介面,如下圖
3. 使用者資訊輸入錯誤時,進行相應的提示,如下圖
5.使用者登入 如果賬戶剩餘金額大於等於2000RMB。則跳到廣告介面,如:使用者Jack餘額為2000RMB,登入的返回頁面如下
4. 當賬戶剩餘金額小於0RMB,則調到警告介面
使用者jack餘額為 0 RMB,登入的返回頁面如下
5. 當賬戶餘額在0RMB和2000RMB之間,則調到正常歡迎介面
如:使用者adear餘額為:10RMB登入的返回頁面如下
五 主要程式碼
1. 資料庫連線類: my_connection
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class my_connection {
public Connection get_connection() {
Connection conn = null;
String name = "root";
String password = "518189";
try {
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/back?charaterEncoding=utf-8";
conn =DriverManager.getConnection(url, name, password);
} catch (Exception E) {
}
return conn;
}
public void closeDB(ResultSet rs, Statementsta, Connection conn) {
try {
rs.close();
sta.close();
conn.close();
} catch (Exception E) {
}
}
}
2.
1. VO:使用者實體類 users
packageVO;
publicclass users {
private String name;
private String password ;
private Double balance ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
}
2. DAO:資料訪問物件: use_dao
package DAO;
import VO.*;
import db.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.*;
public class use_dao {
public void insertUsers(users u ){
my_connection db=new my_connection();
Connection conn=null;
Statement sta=null;
try {
conn=db.get_connection();
sta=conn.createStatement();
String sql="insert intouser(name,password)values('"+u.getName()+"','"+u.getPassword()+"')";
sta.executeUpdate(sql);
}catch (Exception E){
}finally {
}
}
public ResultSet select(users u){
my_connection db=new my_connection();
Connection conn=null;
PreparedStatement sta=null;
ResultSet rs=null;
String uname=u.getName();
String upassword=u.getPassword();
try {
String sql="select * from userwhere name=? and pwd=?";
conn=db.get_connection();
sta=conn.prepareStatement(sql);
sta.setObject(1,uname);
rs= sta.executeQuery(sql);
}catch (Exception E){
}finally {
}
return rs;
}
}
3. servlet: user_servlet
package servlet
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import VO.users;
import DAO.use_dao;
public class user_servlet extendsHttpServlet {
protected void doPost(HttpServletRequestrequest, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequestrequest, HttpServletResponse response) throws ServletException, IOException {
String uname =request.getParameter("name");
String upassword =request.getParameter("password");
users u = new users();
u.setName(uname);
u.setPassword(upassword);
use_dao dao = new use_dao();
ResultSet RS = dao.select(u);
try{ if (RS.next()) {
Doublemoney=RS.getDouble("money");
if(money<=0){
request.getRequestDispatcher("/alter.jsp").forward(request,response);
}elseif(0<money&&money<2000){
request.setAttribute("money",money);
request.setAttribute("name",uname);
request.getRequestDispatcher("/view.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/high.jsp").forward(request,response);
}
}
}catch (Exception E){
}
}
4. 銀行系統登入介面:login.jsp
<%--
Created by IntelliJ IDEA.
User: 王志華
Date: 17-11-17
Time: 下午7:09
To change this template use File | Settings |File Templates.
--%>
<%@ pagecontentType="text/html;charset=UTF-8" pageEncoding="utf-8"language="java" %>
<html>
<head>
<meta charset="utf-8">
<title > 王志華的設計頁面</title>
<scripttype="text/javascript">
window.onload=function()
{
varbt=document.getElementById("p3");
bt.onclick=function()
{
if(document.myform.name.value=="")
{
alert("使用者名稱不能為空!");
document.myform.name.focus();
return false;
}
elseif(document.myform.password.value=="")
{
alert("密碼不能為空!");
document.myform.password.focus();
return false;
}
}
}
</script>
<style >
form{
width: 1200px; height: 800px;background:-webkit-linear-gradient(#4686de 0%, #ffffff 100%);padding: 20px;
}
input{ width: 200px ;height: 30px;
background: seashell }
input:focus{background: whitesmoke}
<!--顏色加文字框一起加聚焦-->
a{ color:#ff32d1;width: 200px;height: 50px}
legend { color:black;font-size:40px;font-style: normal;
}
#p3{width: 60px;height: 30px;background: powderblue;}
#p4{width: 60px;height: 30px;background: powderblue;}
#p5{width: 60px;height: 30px ;background:powderblue;}
</style>
</head>
<bodybgcolor="#e3e3e3">
<center>
<form action="user_select"method="get" name="myform">
<fieldset >
<legendalign="center">銀行系統</legend>
<div>
<labelfor="p1">賬號:</label>
<input id="p1"type="text" name="name">
</div>
<br>
<div>
<labelfor="p2">密碼:</label>
<inputid="p2" type="password" name="password">
</div>
<div>
<tablealign="center">
<tr>
<td> <input type="submit" id="p3"value="登入"/></td>
<td><input type="reset"id="p4" value="重置"/></td>
<td><input type="button" id="p5"onclick="window.location.href='register.jsp'"class="searchbut" value="註冊" ></td>
</tr>
</table>
</div>
</fieldset>
</form>
</center>
</body>
</html>
5. 廣告介面 high_page.jsp
<%@ pagecontentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<style>
h1{
font-size: xx-large;color: #ff32d1;
}
</style>
</head>
<body>
<center>
<h1>人生如此美好,何不今朝購物今朝醉</h1>
<img src="/photo/p1.jpg">
</center>
</body>
</html>
6. 警告介面,alter_page.jsp
<%@ pagecontentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<style>
body{
background:url("photo/p3.jpg");
}
</style>
</head>
<body>
<center>
<h1>請你儘快還錢</h1>
<img src="/photo/p2.jpg">
</center>
</body>
</htmlJPG
7. 正常登入介面 view.jsp
<%--
Created by IntelliJ IDEA.
User: apple
Date: 17-12-1
Time: 下午4:07
To change this template use File | Settings |File Templates.
--%>
<%@ pagecontentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body BGCOLOR="ffeefe">
<center>
歡迎<% request.getParameter("name")%>
您的餘額為:<% out.print(request.getParameter("money"));
%>:RMB
</center>
</body>
</html>