java?Web實(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)目

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("123456")) {
// 跳轉(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")%>,登錄失??!</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)到登錄頁面】超鏈接

輸入正確的用戶名和密碼(易烊千璽:123456)

單擊【登錄】按鈕,跳轉(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.xyx.serlvet包,在包里創(chuàng)建 LoginServlet 類
package net.xyx.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;
@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);
}
}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)到登錄頁面】超鏈接

輸入正確的用戶名和密碼(易烊千璽:001128)
單擊【登錄】按鈕,跳轉(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

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.xyx.bean包,然后在包里創(chuàng)建User類,跟用戶表(t_user)對(duì)應(yīng),簡(jiǎn)稱ORM

package net.xyx.bean;
import java.util.Date;
/**
* 功能:用戶實(shí)體類
* 作者:xyx
* 日期: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)目


6、創(chuàng)建數(shù)據(jù)庫連接管理工具類
創(chuàng)建net.xyx.dbutils包,在包里創(chuàng)建 ConnectionManager 類

package net.xyx.dbutils;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 功能:數(shù)據(jù)庫連接管理類
* 作者:xyx
* 日期:2020年06月05日
*/
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/student/test?useSSL=false"; // 數(shù)據(jù)庫統(tǒng)一資源標(biāo)識(shí)符
private static final String USER = "root"; // 數(shù)據(jù)庫用戶
private static final String PASSWORD = "1"; // 數(shù)據(jù)庫密碼
//私有化構(gòu)造方法,拒絕實(shí)例化
private ConnectionManager() {
}
/**
* 獲取數(shù)據(jù)庫連接靜態(tài)方法
*
* @return 數(shù)據(jù)庫連接對(duì)象
*/
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)方法
*
* @param conn
*/
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();
}
}
}
/**
* 主方法:測(cè)試兩個(gè)靜態(tài)方法
*
* @param args
*/
public static void main(String[] args) {
// 獲取數(shù)據(jù)庫連接
Connection conn = getConnection();
// 判斷數(shù)據(jù)庫連接是否成功
if (conn != null) {
JOptionPane.showMessageDialog(null, "恭喜,數(shù)據(jù)庫連接成功!");
} else {
JOptionPane.showMessageDialog(null, "遺憾,數(shù)據(jù)庫連接失?。?);
}
// 關(guān)閉數(shù)據(jù)庫連接
closeConnection(conn);
}
}7、創(chuàng)建用戶數(shù)據(jù)訪問類
在net.xyx根包里創(chuàng)建dao子包,然后在子包里創(chuàng)建 UserDao 類
package net.xyx.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ù)訪問類
* 作者:xyx
* 日期: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.xyx根包里創(chuàng)建test子類,在子包里創(chuàng)建TestUser
package net.xyx.test;
import net.xyx.bean.User;
import net.xyx.dao.UserDao;
import org.junit.Test;
/**
* 功能:測(cè)試用戶數(shù)據(jù)訪問類
* 作者:xyx
* 日期: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)行程序,提示登錄失敗
四、采用MVC模式實(shí)現(xiàn)用戶注冊(cè)功能
1、創(chuàng)建Web項(xiàng)目
創(chuàng)建Java Enterprise項(xiàng)目,添加Web Application功能

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

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

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

2、創(chuàng)建內(nèi)容

首頁

注冊(cè)界面

隨后注冊(cè)成功和失敗都會(huì)彈出相應(yīng)的界面
總結(jié)
到此這篇關(guān)于java Web實(shí)現(xiàn)用戶登錄功能的文章就介紹到這了,更多相關(guān)java Web用戶登錄功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA中String類與StringBuffer類的區(qū)別
這篇文章主要為大家詳細(xì)介紹了JAVA中String類與StringBuffer類的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Java設(shè)計(jì)模式的策略模式簡(jiǎn)析
這篇文章主要介紹了Java設(shè)計(jì)模式的策略模式簡(jiǎn)析,策略模式中定義了一系列的算法族,算法族指的是類似于一系列的行為、策略,策略模式將一系列的行為封裝成類,既可以說是將每一種相類似的行為都封裝成一個(gè)類,也有可能存在特殊的不進(jìn)行封裝的行為,需要的朋友可以參考下2023-12-12
Springboot JPA打印SQL語句及參數(shù)的實(shí)現(xiàn)
在SpringBoot項(xiàng)目中調(diào)試和優(yōu)化數(shù)據(jù)庫操作是很常見的需求,本文主要介紹了Springboot JPA打印SQL語句及參數(shù)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
java基于jcifs.smb實(shí)現(xiàn)遠(yuǎn)程發(fā)送文件到服務(wù)器
這篇文章主要介紹了java基于jcifs.smb實(shí)現(xiàn)遠(yuǎn)程發(fā)送文件到服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
學(xué)會(huì)Java字節(jié)碼指令,成為技術(shù)大佬
Java 字節(jié)碼指令是 JVM 體系中非常難啃的一塊硬骨頭,我估計(jì)有些讀者會(huì)有這樣的疑惑,“Java 字節(jié)碼難學(xué)嗎?我能不能學(xué)會(huì)?。俊北疚膸ьI(lǐng)大家一探究竟,幫助大家搞懂java底層代碼如何執(zhí)行2021-08-08
intellij idea修改maven配置時(shí)總是恢復(fù)默認(rèn)配置的解決方法idea版本(2020.2.x)
這篇文章主要介紹了intellij idea修改maven配置時(shí)總是恢復(fù)默認(rèn)配置的解決方法idea版本(2020.2.x),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
SpringMVC 上傳文件 MultipartFile 轉(zhuǎn)為 File的方法
這篇文章主要介紹了SpringMVC 上傳文件 MultipartFile 轉(zhuǎn)為 File的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

