欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能全過程

 更新時(shí)間:2024年01月18日 15:49:18   作者:howard2005  
在Java開發(fā)過程中實(shí)現(xiàn)用戶的注冊(cè)功能是最基本的,這篇文章主要給大家介紹了關(guān)于Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下

零、本節(jié)學(xué)習(xí)目標(biāo)

  • 掌握純JSP方式實(shí)現(xiàn)用戶登錄功能
  • 掌握J(rèn)SP+Servlet方式實(shí)現(xiàn)用戶登錄功能
  • 掌握J(rèn)SP+Servlet+DB方式實(shí)現(xiàn)用戶登錄功能
  • 掌握MVC模式實(shí)現(xiàn)用戶登錄功能

一、純JSP方式實(shí)現(xiàn)用戶登錄功能

(一)實(shí)現(xiàn)思路

登錄頁面login.jsp,輸入用戶名和密碼后,跳轉(zhuǎn)到登錄處理頁面doLogin.jsp進(jìn)行業(yè)務(wù)邏輯處理,登錄成功,跳轉(zhuǎn)到登錄成功頁面success.jsp,否則跳轉(zhuǎn)到登錄失敗頁面failure.jsp。

(二)實(shí)現(xiàn)步驟

1、創(chuàng)建Web項(xiàng)目

創(chuàng)建Java Enterprise項(xiàng)目,添加Web Application功能

設(shè)置項(xiàng)目名與保存位置

單擊【Finish】按鈕

在項(xiàng)目結(jié)構(gòu)窗口里修改Artifact名 - LoginDemo01

編輯服務(wù)器配置,重新部署項(xiàng)目

切換到【Server】選項(xiàng)卡

2、創(chuàng)建登錄頁面

登錄頁面 - login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>用戶登錄</title>
    </head>
    <body>
        <h3 style="text-align: center">用戶登錄</h3>
        <form action="doLogin.jsp" method="post">
            <table border="1" cellpadding="10" style="margin: 0px auto">
                <tr>
                    <td align="center">賬號(hào)</td>
                    <td><input type="text" name="username"/></td>
                </tr>
                <tr>
                    <td align="center">密碼</td>
                    <td><input type="password" name="password"/></td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="登錄"/>
                        <input type="reset" value="重置"/>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

3、創(chuàng)建登錄處理頁面

登錄處理頁面 - doLogin.jsp

<%
    // 獲取登錄表單數(shù)據(jù)
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    // 判斷登錄是否成功
    if (username.equals("無心劍") && password.equals("903213")) {
        // 跳轉(zhuǎn)到登錄成功頁面,傳遞用戶名
        response.sendRedirect("success.jsp?username=" + username);
    } else {
        // 跳轉(zhuǎn)到登錄失敗頁面,傳遞用戶名
        response.sendRedirect("failure.jsp?username=" + username);
    }
%>

4、創(chuàng)建登錄成功頁面

登錄成功頁面 - success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>登錄成功</title>
    </head>
    <body>
        <h3 style="text-align: center">恭喜,<%=request.getParameter("username")%>,登錄成功!</h3>
    </body>
</html>

5、創(chuàng)建登錄失敗頁面

登錄失敗頁面 - failure.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>登錄失敗</title>
    </head>
    <body>
        <h3 style="text-align: center">遺憾,<%=request.getParameter("username")%>,登錄失?。?lt;/h3>
    </body>
</html>

6、編輯項(xiàng)目首頁

項(xiàng)目首頁 - index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>首頁</title>
    </head>
    <body>
        <h1 style="color: red; text-align: center">純JSP方式實(shí)現(xiàn)用戶登錄功能</h1>
        <h3 style="text-align: center"><a href="login.jsp" rel="external nofollow"  rel="external nofollow" >跳轉(zhuǎn)到登錄頁面</a></h3>
    </body>
</html>

(三)測(cè)試結(jié)果

啟動(dòng)服務(wù)器,顯示首頁

單擊【跳轉(zhuǎn)到登錄頁面】超鏈接

輸入正確的用戶名和密碼(無心劍:903213)

單擊【登錄】按鈕,跳轉(zhuǎn)到登錄成功頁面

返回登錄頁面,輸入錯(cuò)誤的用戶名或密碼

單擊【登錄】按鈕,跳轉(zhuǎn)到登錄失敗頁面

操作錄屏

二、JSP+Servlet方式實(shí)現(xiàn)用戶登錄功能

(一)實(shí)現(xiàn)思路

登錄頁面login.jsp,輸入用戶名和密碼后,跳轉(zhuǎn)到登錄處理程序LoginServlet進(jìn)行業(yè)務(wù)邏輯處理,登錄成功,跳轉(zhuǎn)到登錄成功頁面success.jsp,否則跳轉(zhuǎn)到登錄失敗頁面failure.jsp。

(二)實(shí)現(xiàn)步驟

1、創(chuàng)建Web項(xiàng)目

創(chuàng)建Java Enterprise項(xiàng)目,添加Web Application功能

設(shè)置項(xiàng)目名與保存位置

單擊【Finish】按鈕

在項(xiàng)目結(jié)構(gòu)窗口里修改Artifact名 - LoginDemo02

編輯服務(wù)器配置,重新部署項(xiàng)目

切換到【Server】選項(xiàng)卡

2、創(chuàng)建登錄頁面

登錄頁面 - login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>用戶登錄</title>
    </head>
    <body>
        <h3 style="text-align: center">用戶登錄</h3>
        <form action="login" method="post">
            <table border="1" cellpadding="10" style="margin: 0px auto">
                <tr>
                    <td align="center">賬號(hào)</td>
                    <td><input type="text" name="username"/></td>
                </tr>
                <tr>
                    <td align="center">密碼</td>
                    <td><input type="password" name="password"/></td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="登錄"/>
                        <input type="reset" value="重置"/>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

3、創(chuàng)建登錄處理程序

創(chuàng)建net.huawei.serlvet包,在包里創(chuàng)建LoginServlet

package net.huawei.servlet;

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 java.io.IOException;
import java.net.URLEncoder;

/**
 * 功能:登錄處理程序
 * 作者:華衛(wèi)
 * 日期:2023年05月03日
 */
@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 設(shè)置請(qǐng)求對(duì)象字符編碼格式
        request.setCharacterEncoding("utf-8");
        // 獲取登錄表單數(shù)據(jù)
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 判斷登錄是否成功
        if (username.equals("無心劍") && password.equals("903213")) {
            // 采用重定向,跳轉(zhuǎn)到登錄成功頁面
            response.sendRedirect("success.jsp?username=" + URLEncoder.encode(username, "utf-8"));
        } else {
            // 采用重定向,跳轉(zhuǎn)到登錄失敗頁面
            response.sendRedirect("failure.jsp?username=" + URLEncoder.encode(username, "utf-8"));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}

說明:必須設(shè)置請(qǐng)求對(duì)象的字符編碼為utf-8,否則輸入正確用戶名和密碼也會(huì)登錄失敗。重定向傳遞用戶名參數(shù)時(shí),必須采用URLEncoder類的encode方法進(jìn)行編碼,否則程序運(yùn)行會(huì)報(bào)錯(cuò)。

對(duì)應(yīng)關(guān)系圖

4、創(chuàng)建登錄成功頁面

登錄成功頁面 - success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>登錄成功</title>
    </head>
    <body>
        <h3 style="text-align: center">恭喜,<%=request.getParameter("username")%>,登錄成功!</h3>
    </body>
</html>

5、創(chuàng)建登錄失敗頁面

登錄失敗頁面 - failure.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>登錄失敗</title>
    </head>
    <body>
        <h3 style="text-align: center">遺憾,<%=request.getParameter("username")%>,登錄失敗!</h3>
    </body>
</html>

6、編輯項(xiàng)目首頁

項(xiàng)目首頁 - index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>首頁</title>
    </head>
    <body>
        <h1 style="color: red; text-align: center">JSP+Servlet方式實(shí)現(xiàn)用戶登錄功能</h1>
        <h3 style="text-align: center"><a href="login.jsp" rel="external nofollow"  rel="external nofollow" >跳轉(zhuǎn)到登錄頁面</a></h3>
    </body>
</html>

(三)測(cè)試結(jié)果

啟動(dòng)服務(wù)器,顯示首頁

單擊【跳轉(zhuǎn)到登錄頁面】超鏈接

輸入正確的用戶名和密碼(無心劍:903213)

單擊【登錄】按鈕,跳轉(zhuǎn)到登錄成功頁面

返回登錄頁面,輸入錯(cuò)誤的用戶名或密碼

單擊【登錄】按鈕,跳轉(zhuǎn)到登錄失敗頁面

操作錄屏

三、JSP+Servlet+DB方式實(shí)現(xiàn)用戶登錄功能

(一)實(shí)現(xiàn)思路

總體上采用MVC架構(gòu)。登錄頁面login.jsp,輸入用戶名和密碼后,跳轉(zhuǎn)到登錄處理程序LoginServlet進(jìn)行業(yè)務(wù)邏輯處理,調(diào)用服務(wù)層,服務(wù)層調(diào)用數(shù)據(jù)訪問層(DAO),連接數(shù)據(jù)庫,查詢數(shù)據(jù)庫,以此判斷是否登錄成功。登錄成功,跳轉(zhuǎn)到登錄成功頁面success.jsp,否則跳轉(zhuǎn)到登錄失敗頁面failure.jsp。

MVC 是 Model、View 和 Controller 的縮寫,分別代表 Web 應(yīng)用程序中的 3 種職責(zé)。

(二)實(shí)現(xiàn)步驟

1、創(chuàng)建數(shù)據(jù)庫

創(chuàng)建數(shù)據(jù)庫 - test

單擊【確定】按鈕

2、創(chuàng)建用戶表

創(chuàng)建用戶表結(jié)構(gòu) - t_user

CREATE TABLE `t_user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶ID',
  `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '用戶名',
  `password` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '用戶密碼',
  `telephone` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '聯(lián)系電話',
  `register_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '注冊(cè)時(shí)間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

插入用戶記錄

INSERT INTO `t_user` VALUES (1, '無心劍', '12345', '15888781123', '2023-01-06 07:20:46');
INSERT INTO `t_user` VALUES (2, '陳燕文', '11111', '13990901140', '2023-02-06 11:40:49');
INSERT INTO `t_user` VALUES (3, '鄭素平', '22222', '15845678907', '2023-05-01 08:30:53');

3、創(chuàng)建Web項(xiàng)目

創(chuàng)建Java Enterprise項(xiàng)目,添加Web Application功能

設(shè)置項(xiàng)目名與保存位置

單擊【Finish】按鈕

在項(xiàng)目結(jié)構(gòu)窗口里修改Artifact名 - LoginDemo03

編輯服務(wù)器配置,重新部署項(xiàng)目

切換到【Server】選項(xiàng)卡

4、創(chuàng)建用戶實(shí)體類

創(chuàng)建net.huawei.bean包,然后在包里創(chuàng)建User類,跟用戶表(t_user)對(duì)應(yīng),簡(jiǎn)稱ORM(Object Relation Mapping)

package net.huawei.bean;

import java.util.Date;

/**
 * 功能:用戶實(shí)體類
 * 作者:華衛(wèi)
 * 日期:2023年05月19日
 */
public class User {
    private int id;
    private String username;
    private String password;
    private String telephone;
    private Date registerTime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", telephone='" + telephone + '\'' +
                ", registerTime=" + registerTime +
                '}';
    }
}

5、添加數(shù)據(jù)庫驅(qū)動(dòng)程序

WEB-INF目錄下創(chuàng)建lib目錄,添加數(shù)據(jù)庫驅(qū)動(dòng)程序

將數(shù)據(jù)庫驅(qū)動(dòng)程序(jar包)作為庫添加到項(xiàng)目

單擊【Add as Library…】

單擊【OK】按鈕

6、創(chuàng)建數(shù)據(jù)庫連接管理工具類

創(chuàng)建net.huawei.dbutils包,在包里創(chuàng)建ConnectionManager

package net.huawei.dbutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 功能:數(shù)據(jù)庫連接管理類
 * 作者:華衛(wèi)
 * 日期:2023年05月19日
 */
public class ConnectionManager {
    private static final String DRIVER = "com.mysql.jdbc.Driver"; // 數(shù)據(jù)庫驅(qū)動(dòng)程序
    private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8"; // 數(shù)據(jù)庫統(tǒng)一資源標(biāo)識(shí)符
    private static final String USER = "root"; // 數(shù)據(jù)庫用戶
    private static final String PASSWORD = "903213"; // 數(shù)據(jù)庫密碼(記住改成自己的數(shù)據(jù)庫密碼)

    // 私有化構(gòu)造方法,拒絕實(shí)例化
    private ConnectionManager() {
    }

    // 獲取數(shù)據(jù)庫連接靜態(tài)方法
    public static Connection getConnection() {
        // 定義數(shù)據(jù)庫連接
        Connection conn = null;

        try {
            // 安裝數(shù)據(jù)庫驅(qū)動(dòng)程序
            Class.forName(DRIVER);
            // 獲取數(shù)據(jù)庫連接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 返回?cái)?shù)據(jù)庫連接
        return conn;
    }

    // 關(guān)閉數(shù)據(jù)連接靜態(tài)方法
    public static void closeConnection(Connection conn) {
        // 判斷數(shù)據(jù)庫連接是否非空
        if (conn != null) {
            try {
                // 判斷連接是否未關(guān)閉
                if (!conn.isClosed()) {
                    // 關(guān)閉數(shù)據(jù)庫連接
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        // 獲取數(shù)據(jù)庫連接
        Connection conn = getConnection();
        // 判斷數(shù)據(jù)庫連接是否成功
        if (conn != null) {
            System.out.println("恭喜,數(shù)據(jù)庫連接成功~");
        } else {
            System.out.println("遺憾,數(shù)據(jù)庫連接失敗~");
        }
        // 關(guān)閉數(shù)據(jù)庫連接
        closeConnection(conn);
        System.out.println("數(shù)據(jù)庫連接已經(jīng)關(guān)閉~");
    }
}

運(yùn)行程序,查看結(jié)果

7、創(chuàng)建用戶數(shù)據(jù)訪問類

net.huawei根包里創(chuàng)建dao子包,然后在子包里創(chuàng)建UserDao

package net.huawei.dao;

import net.huawei.bean.User;
import net.huawei.dbutils.ConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 功能:用戶數(shù)據(jù)訪問類
 * 作者:華衛(wèi)
 * 日期:2023年05月19日
 */
public class UserDao {
    /**
     * 用戶登錄方法
     * @param username
     * @param password
     * @return 用戶對(duì)象(非空:登錄成功,否則登錄失敗)
     */
    public User login(String username, String password) {
        // 聲明用戶對(duì)象
        User user = null;

        // 獲取數(shù)據(jù)庫連接
        Connection conn = ConnectionManager.getConnection();
        try {
            // 定義SQL字符串
            String strSQL = "SELECT * FROM t_user WHERE username = ? AND password = ?";
            // 創(chuàng)建預(yù)備語句對(duì)象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 設(shè)置占位符
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            // 執(zhí)行查詢,返回結(jié)果集
            ResultSet rs = pstmt.executeQuery();
            // 判斷結(jié)果集是否為空
            if (rs.next()) {
                // 創(chuàng)建用戶對(duì)象
                user = new User();
                // 利用當(dāng)前記錄字段值來設(shè)置用戶對(duì)象屬性
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTelephone(rs.getString("telephone"));
                user.setRegisterTime(rs.getTimestamp("register_time"));
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        } finally {
            // 關(guān)閉數(shù)據(jù)庫連接
            ConnectionManager.closeConnection(conn);
        }

        // 返回用戶對(duì)象
        return user;
    }
}

8、測(cè)試用戶數(shù)據(jù)訪問類

net.huawei根包里創(chuàng)建test子包,在子包里創(chuàng)建TestUserDao

package net.huawei.test;

import net.huawei.bean.User;
import net.huawei.dao.UserDao;
import org.junit.Test;

/**
 * 功能:測(cè)試用戶數(shù)據(jù)訪問類
 * 作者:華衛(wèi)
 * 日期:2023年05月19日
 */
public class TestUserDao {
    @Test
    public void testLogin() {
        String username = "無心劍";
        String password = "12345";

        // 創(chuàng)建用戶數(shù)據(jù)訪問對(duì)象
        UserDao userDao = new UserDao();
        // 調(diào)用登錄方法,返回用戶對(duì)象
        User user = userDao.login(username, password);
        // 判斷用戶登錄是否成功
        if (user != null) { // 成功
            System.out.println("恭喜,用戶[" + username + "]登錄成功~");
        } else { // 失敗
            System.out.println("遺憾,用戶[" + username + "]登錄失敗~");
        }
    }
}

運(yùn)行程序,查看結(jié)果

修改用戶名和密碼,再次運(yùn)行程序,提示登錄失敗

9、創(chuàng)建用戶服務(wù)類

net.huawei根包里創(chuàng)建service子包,在子包里創(chuàng)建UserService

package net.huawei.service;

import net.huawei.bean.User;
import net.huawei.dao.UserDao;

/**
 * 功能:用戶服務(wù)類
 * 作者:華衛(wèi)
 * 日期:2023年05月26日
 */
public class UserService {
    private UserDao userDao = new UserDao();
    
    public User login(String username, String password) {
        return userDao.login(username, password);
    }
}

10、創(chuàng)建登錄處理程序

net.huawei根包里創(chuàng)建servlet子包,在子包里創(chuàng)建LoginServlet

package net.huawei.servlet;

import net.huawei.bean.User;
import net.huawei.service.UserService;

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 java.io.IOException;
import java.net.URLEncoder;

/**
 * 功能:登錄處理程序
 * 作者:華衛(wèi)
 * 日期:2023年05月26日
 */
@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 設(shè)置請(qǐng)求對(duì)象字符編碼格式
        request.setCharacterEncoding("utf-8");
        // 獲取登錄表單數(shù)據(jù)
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 創(chuàng)建用戶服務(wù)對(duì)象
        UserService us = new UserService();
        // 調(diào)用登錄方法,返回用戶對(duì)象
        User user = us.login(username, password);
        // 判斷登錄是否成功
        if (user != null) {
            // 采用重定向,跳轉(zhuǎn)到登錄成功頁面
            response.sendRedirect("success.jsp?username=" + URLEncoder.encode(username, "utf-8"));
        } else {
            // 采用重定向,跳轉(zhuǎn)到登錄失敗頁面
            response.sendRedirect("failure.jsp?username=" + URLEncoder.encode(username, "utf-8"));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}

11、創(chuàng)建登錄頁面

登錄頁面 - login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>用戶登錄</title>
    </head>
    <body>
        <h3 style="text-align: center">用戶登錄</h3>
        <form action="login" method="post">
            <table border="1" cellpadding="10" style="margin: 0px auto">
                <tr>
                    <td align="center">賬號(hào)</td>
                    <td><input type="text" name="username"/></td>
                </tr>
                <tr>
                    <td align="center">密碼</td>
                    <td><input type="password" name="password"/></td>
                </tr>
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="登錄"/>
                        <input type="reset" value="重置"/>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

12、創(chuàng)建登錄成功頁面

登錄成功頁面 - success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>登錄成功</title>
    </head>
    <body>
        <h3 style="text-align: center">恭喜,<%=request.getParameter("username")%>,登錄成功!</h3>
    </body>
</html>

13、創(chuàng)建登錄失敗頁面

登錄失敗頁面 - failure.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>登錄失敗</title>
    </head>
    <body>
        <h3 style="text-align: center">遺憾,<%=request.getParameter("username")%>,登錄失??!</h3>
    </body>
</html>

14、編輯項(xiàng)目首頁

項(xiàng)目首頁 - index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>首頁</title>
    </head>
    <body>
        <h1 style="color: red; text-align: center">JSP+Servlet+DB方式實(shí)現(xiàn)用戶登錄功能</h1>
        <h3 style="text-align: center"><a href="login.jsp">跳轉(zhuǎn)到登錄頁面</a></h3>
    </body>
</html>

(三)測(cè)試結(jié)果

啟動(dòng)服務(wù)器,顯示首頁

單擊【跳轉(zhuǎn)到登錄頁面】超鏈接

輸入正確的用戶名和密碼(無心劍:12345)

單擊【登錄】按鈕,跳轉(zhuǎn)到登錄成功頁面

返回登錄頁面,輸入用戶名和密碼(陳燕文:12345)

單擊【登錄】按鈕,跳轉(zhuǎn)到登錄失敗頁面

登錄操作錄屏演示

四、課后作業(yè)

任務(wù)1、采用MVC模式實(shí)現(xiàn)用戶注冊(cè)功能

創(chuàng)建Web項(xiàng)目 - register

首頁

注冊(cè)頁面

注冊(cè)成功頁面

注冊(cè)失敗頁面

注冊(cè)操作錄屏演示

在Navicat里查看用戶表,看是否添加了新用戶記錄

任務(wù)2、在登錄成功頁面顯示用戶列表

測(cè)試UserDao的findAll()方法

總結(jié) 

到此這篇關(guān)于Java Web應(yīng)用小案例之實(shí)現(xiàn)用戶登錄功能的文章就介紹到這了,更多相關(guān)Java Web實(shí)現(xiàn)用戶登錄功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在Java中操作Zookeeper的示例代碼詳解

    在Java中操作Zookeeper的示例代碼詳解

    這篇文章主要介紹了在Java中操作Zookeeper的示例代碼詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java中枚舉的使用詳解

    Java中枚舉的使用詳解

    這篇文章主要介紹了Java中枚舉的使用詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Java開發(fā)實(shí)例之圖書管理系統(tǒng)的實(shí)現(xiàn)

    Java開發(fā)實(shí)例之圖書管理系統(tǒng)的實(shí)現(xiàn)

    圖書管理的功能大體包括:增加書籍、借閱書籍、刪除書籍、查看書籍列表、退出系統(tǒng)、查找書籍、返還書籍這些,本文主要給大家介紹該系統(tǒng)的數(shù)據(jù)庫語句,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • 關(guān)于Java中HashCode方法的深入理解

    關(guān)于Java中HashCode方法的深入理解

    這篇文章主要給大家介紹了關(guān)于Java中HashCode方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Java實(shí)現(xiàn)解析并生成xml原理實(shí)例詳解

    Java實(shí)現(xiàn)解析并生成xml原理實(shí)例詳解

    這篇文章主要介紹了Java實(shí)現(xiàn)解析并生成xml原理實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java插件擴(kuò)展機(jī)制之SPI案例講解

    Java插件擴(kuò)展機(jī)制之SPI案例講解

    這篇文章主要介紹了Java插件擴(kuò)展機(jī)制之SPI案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Java 兩種延時(shí)thread和timer詳解及實(shí)例代碼

    Java 兩種延時(shí)thread和timer詳解及實(shí)例代碼

    這篇文章主要介紹了Java 兩種延時(shí)thread和timer詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • java避免多層嵌套循環(huán)用到的一些小技巧分享

    java避免多層嵌套循環(huán)用到的一些小技巧分享

    這篇文章主要介紹了java避免多層嵌套循環(huán)用到的一些小技巧分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法

    Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法

    在java中經(jīng)常會(huì)遇到需要對(duì)數(shù)據(jù)進(jìn)行類型轉(zhuǎn)換的場(chǎng)景,這篇文章主要給大家介紹了關(guān)于Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • Java int與integer的對(duì)比區(qū)別

    Java int與integer的對(duì)比區(qū)別

    這篇文章主要介紹了Java int與integer的對(duì)比區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2017-01-01

最新評(píng)論