圖書館系統(登錄設計)


基於MVC設計----圖書館系統(登錄設計)

這一學期學習了JavaWeb開發, 課程結束時老師給的任務是做一個課程設計,博主使用MVC(jsp、servlets、javabean)開發了一個簡單的圖書館在線系統,其中也包含了一些html、css、JavaScript等知識,有興趣的小伙伴可以參觀參觀。現在具體先給出運行截圖,再給出具體項目的實現步驟。由於實現程序源代碼較多,這次先講此程序登錄的MVC設計。

到github下載我的項目

整體項目運行截圖

用戶登錄界面

在這里插入圖片描述

管理員登錄界面

在這里插入圖片描述
學生用戶注冊頁面

在這里插入圖片描述
用戶操作界面(主頁面)

在這里插入圖片描述

用戶操作頁面(主菜單)

在這里插入圖片描述

管理員操縱主頁(主菜單)

在這里插入圖片描述
等界面運行截圖。

登錄設計(MVC設計模式)

請設想,如果用戶想要登錄,肯定要設計一個jsp頁面,然后用戶輸入的數據提交給對應的servlet進行處理,servlet再通過訪問數據庫進行查詢得出登錄結果,返回給servler,servlet根據不同結果進行跳轉或者錯誤信息提示。基本的步驟如下圖所示(第一次畫這種圖,哈哈哈,邏輯可能有點小不清晰):

在這里插入圖片描述

看着是不是有些復雜,哈哈哈,是我畫的雜亂了,邏輯還是比較清楚的,下面給出各個部分的源代碼,但由於關聯數據較多(css、javascript、數據庫文件等),可能效果並不完整,本項目的全部源代碼博主會上傳至github供大家參考使用,嘿嘿。

登錄功能源代碼

login.jsp

<%@ page import="vo.Students" %><%--
  Created by IntelliJ IDEA.
  User: Mr.Gao
  Date: 2020/6/2
  Time: 9:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login Page</title>                                                   <%--標題名稱--%>
    <link rel="stylesheet" type="text/css" href="css/login.css">                <%--導入登錄界面的css樣式--%>

</head>
<body>
    <%
        Cookie[] cookies = request.getCookies();                               /*獲取本機的cookie數據, 以cookie類型的數組返回*/
        String user = "";                                                      /*定義用戶名字符串, 默認為空字符串*/
        String password = "";                                                  /* 定義用戶密碼字符串, 默認為空字符串密碼*/
        String check="checked";                                                /* 定義check數據表示用戶上次登錄選擇的登錄方式,默認為選中*/
        if(cookies != null && cookies.length > 0){                             /* 若成功獲取cookie數組cookies,並且不為null且長度大於0*/

            for(int i = 0; i < cookies.length; i++){                           /* 對cookie內存放的所有數據進行循環遍歷*/
                if(cookies[i].getName().equals("user")){                       /* 若匹配到鍵名為"user"的cookie數據,說明其存取的為該用戶的賬戶數據*/
                    user = cookies[i].getValue();                              /* 獲取鍵值,獲取該用戶的賬號 */
                }
                if(cookies[i].getName().equals("password")){                    /*若匹配到鍵名為"password"的cookie數據,說明其存放的為該用戶的密碼數據*/
                    password = cookies[i].getValue();                           /*獲取鍵值,獲取該用戶的密碼*/
                }
                if(cookies[i].getName().equals("check")){                       /*若匹配到鍵名為"check"的cookie數據,為上次用戶選擇學生登錄或管理員登錄的信息*/
                    check = cookies[i].getValue();                              /*獲取鍵值,獲取登錄狀態*/
                }
            }
        }

        Students student = (Students) session.getAttribute("student");          //每次返回登錄界面都獲取session對象中的學生用戶
        if(student != null){                                                            //若student對象存在,說明此時為退出登錄狀態,則進行以下操作
            session.removeAttribute("student");                                         //移除session對象中的用戶信息
            String person = (String)application.getAttribute("person");                 //獲取application對象中的所有用戶數量,為字符串類型
            application.setAttribute("person",(Integer.parseInt(person)-1) + "");       //重新設置application對象中的用戶數量,為原來數據減1,字符串類型存入
        }

    %>
    <div class="htmleaf-container">
        <div class="login-wrap">                            <%-- 登錄界面的div塊--%>
            <div class="login-html">
                <h2>圖書館系統</h2>                          <%--二級標題, 登錄信息--%>

                <%--分為兩種登錄方式, 學生登錄和管理員身份登錄--%>
                <input id="tab-1" type="radio" name="tab" class="sign-in" checked>
                <label for="tab-1" class="tab">學生</label>
                <input id="tab-2" type="radio" name="tab" class="sign-up" ${requestScope.check}>     <%--若獲取的請求信息為checked時,下次以管理員登錄為默認--%>
                <label for="tab-2" class="tab">管理員</label>
                <div class="login-form">
                    <div class="sign-in-htm">
                        <form action="userLoginServlet" method="post" name="form_user">            <%--學生學號登錄的表單,提交給userLoginServlet進行處理--%>
                            <div class="group">
                                <label class="label">學號</label>
                                <input name="user" type="text" class="input" placeholder="輸入您的學號,9位數字" autocomplete="off" value="<%=user%>">
                            </div>
                            <div class="group">
                                <label class="label">密碼</label>
                                <input name="password" type="password" class="input" placeholder="輸入您的登錄密碼" autocomplete="off" value="<%=password%>">
                            </div>
                            <div class="group">
                                <input id="check" type="checkbox" class="check" name="check" <%=check%>>
                                <label for="check"><span class="icon"></span> 記住密碼</label>
                                <a href="reg.jsp" class="reg">沒有學號?立即注冊</a>
                                <p class="error">&nbsp;${requestScope.msg}</p>              <%--若用戶登錄失敗,將在此顯示用戶登錄失敗的信息--%>
                            </div>
                            <div class="group">
                                <%--以button方式進行javascript點擊函數處理,驗證登錄格式,並提交--%>
                                <input type="button" class="button" onclick="onclick_user()" value="登錄">
                            </div>
                        </form>
                    </div>
                    <div class="sign-up-htm">
                        <form action="adminLoginServlet" method="post" name="form_admin">   <%--管理員登錄的表單,提交給AdminLoginServlet進行處理--%>
                            <div class="group">
                                <label class="label">管理員賬號</label>
                                <input name="a_user" type="text" class="input" placeholder="輸入管理員賬號,9位數字" autocomplete="off">
                            </div>
                            <div class="group">
                                <label class="label">管理員密碼</label>
                                <input name="a_password" type="password" class="input" placeholder="輸入您的管理員密碼" autocomplete="off">
                            </div>
                            <div class="group">
                                <p>暫不支持記住密碼及注冊</p>
                                <p class="error">&nbsp;${requestScope.a_msg}</p>            <%--若管理員登錄失敗,將在此顯示管理員登錄失敗的信息--%>
                            </div>
                            <div class="group">
                                <%--以button方式進行javascript點擊函數處理,驗證管理員登錄格式,並提交--%>
                                <input type="button" class="button" onclick="onclick_admin()" value="登錄">
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="js/login.js"></script>             <%--導入javascript登錄驗證函數js文件--%>
</body>
</html>

UserLoginServlet.java(用戶提交servlet操作)

package servlets;


import dao.StudentDAO;
import vo.Students;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns = "/userLoginServlet")
public class UserLoginServlet extends HttpServlet {

    @Override           //覆寫Servlet類的doGet()方法
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");                      //設置req對象接收的數據以utf-8編碼格式進行編碼
        String user = req.getParameter("user");             //獲取提交的用戶名
        String password = req.getParameter("password");     //獲取提交的密碼
        String check = req.getParameter("check");           //獲取記住密碼是否選中按鈕狀態
        HttpSession session = req.getSession();                //獲取session的對象
        StudentDAO s_dao = new StudentDAO();                    //實例化學生dao類對象,對學生數據庫表中的學生信息進行操作
        try{
            if(s_dao.isExist(user,password)){               //通過調用dao對象的isExist()方法,判斷此用戶在學生數據庫表中是否存在
                    //若存在,則進行以下操作
                  if(check != null){                        //若check不為null,說明用戶點擊記住密碼按鈕
                      Cookie user_cookie = new Cookie("user",user);                         //將用戶名、密碼、選中狀態存入cookie中,以便下次瀏覽器直接讀取
                      Cookie password_cookie = new Cookie("password",password);
                      Cookie check_cookie = new Cookie("check","checked");
                      user_cookie.setMaxAge(7*24*60*60);                                        //設置生存周期為1周
                      password_cookie.setMaxAge(7*24*60*60);
                      check_cookie.setMaxAge(7*24*60*60);
                      resp.addCookie(user_cookie);                                              //存入resp對象中
                      resp.addCookie(password_cookie);
                      resp.addCookie(check_cookie);
                  }else{                                                                        //否則表示用戶未選擇記住密碼
                      Cookie user_cookie = new Cookie("user",user);
                      Cookie password_cookie = new Cookie("password",password);
                      Cookie check_cookie = new Cookie("check","");
                      user_cookie.setMaxAge(0);                                             //設置其生命周期為0,即無數據
                      password_cookie.setMaxAge(0);
                      check_cookie.setMaxAge(0);
                      resp.addCookie(user_cookie);                                          //存入resp對象中
                      resp.addCookie(password_cookie);
                      resp.addCookie(check_cookie);
                  }
                  Students student = s_dao.getStudentByName(user);                          //通過學生學號,過去該學生對象
                  ServletContext application = this.getServletContext();                    //獲取服務器的application對象
                  String person = (String)application.getAttribute("person");           //取得application對象中的person數據,表示當前學生在線人數
                  if(person == null){                                                   //若為null,表示此為第一個用戶
                      person = "1";                                                     //person初始化為1
                  }else{
                      person = (Integer.parseInt(person) + 1) + "";                     //否則person累加,以字符串形式存儲
                  }


                  session.setAttribute("student", student);                         //將用戶信息存入session對象中
                  application.setAttribute("person",person);                        //將當前在線人數存入application對象中,供在線人數實時更新
                  resp.sendRedirect(req.getContextPath() + "/user.jsp");            //跳轉到學生操縱主頁
            }else {
                                                                                        //若數據庫中無該學生信息,說明登陸失敗
                String msg = (String)req.getAttribute("msg");

                if(msg == null) msg = "賬號或密碼輸入有誤,請重試!";                         //賦值錯誤信息
                req.setAttribute("msg",msg);                                            //存入req對象中,返還給用戶提示

                req.getRequestDispatcher("/login.jsp").forward(req,resp);            //繼續跳轉到登錄界面
            }
        }catch (Exception e){                                           //出現異常,數據庫查詢失敗
            System.out.println("數據庫查詢失敗!");
        }

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

AdminLoginServlet.java(管理員登錄提交操作)

package servlets;

import com.sun.deploy.net.HttpRequest;
import dao.AdminDAO;
import vo.Admins;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/adminLoginServlet")
public class AdminLoginServlet extends HttpServlet {

    @Override//覆寫doGet()方法
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String a_user = req.getParameter("a_user");                 //獲取管理員提交的用戶名、密碼
        String a_password = req.getParameter("a_password");
        HttpSession session = req.getSession();                         //獲取session對象

        AdminDAO a_dao = new AdminDAO();                                //實例化管理員數據庫表的操縱對象
        Admins admin = null;                                            //admin對象為null
        try{
            if(a_dao.isExist(a_user,a_password)){                       //調用dao類判斷該管理員在數據庫管理員表中是否存在
                admin = a_dao.getAdminByUser(a_user);                   //若存在,獲取該管理員對象
                session.setAttribute("admin",admin);                //存入session對象中
                resp.sendRedirect(req.getContextPath() + "/admin.jsp");             //跳轉到管理員主頁面
            }else{
                                                                                //若數據庫管理員表中查詢失敗
                String a_msg = (String) req.getAttribute("a_msg");
                String check = (String) req.getAttribute("check");
                a_msg = "管理員賬戶或密碼輸入錯誤!";                                //設置查詢失敗信息
                check = "checked";                                               //管理員登錄方式鎖定
                req.setAttribute("a_msg",a_msg);                            //將它們存取req對象中,返回給客戶端
                req.setAttribute("check",check);

                req.getRequestDispatcher("/login.jsp").forward(req,resp);           //跳轉到login.jsp登錄界面
            }
        }catch (Exception e){                                                   //出現異常狀況,數據庫查詢失敗
            System.out.println("數據庫管理員表查詢錯誤!");
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}


以上代碼就是登錄設計的主要jsp和java代碼,其中涉及JavaBean的DAO操作類,由於代碼過多,在此不過多進行展示,有興趣的小伙伴可以下載本項目測試哦,哈哈!

總結

這就是圖書館系統的登陸界面設計,小伙伴只要掌握了MVC設計的這種原理,相信千千萬萬個系統都會做出來,哈哈哈,給自己充充電去!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM