java?Web實現(xiàn)用戶登錄功能圖文教程
一、純JSP方式實現(xiàn)用戶登錄功能
(一)實現(xiàn)思路
登錄頁面login.jsp,輸入用戶名和密碼后,跳轉(zhuǎn)到登錄處理頁面doLogin.jsp進行業(yè)務邏輯處理,登錄成功,跳轉(zhuǎn)到登錄成功頁面success.jsp,否則跳轉(zhuǎn)到登錄失敗頁面failure.jsp。
(二)實現(xiàn)步驟
1、創(chuàng)建Web項目
創(chuàng)建 Java Enterprise
項目,添加Web Application功能
設(shè)置項目名與保存位置
單擊【Finish】按鈕
在項目結(jié)構(gòu)窗口里修改Artifact名 - LoginDemo01
編輯服務器配置,重新部署項目
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">賬號</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")%>,登錄失?。?lt;/h3> </body> </html>
6、編輯項目首頁
項目首頁 - 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方式實現(xiàn)用戶登錄成功能</h1> <h3 style="text-align: center"><a href="login.jsp" rel="external nofollow" rel="external nofollow" >跳轉(zhuǎn)到登錄頁面</a></h3> </body> </html>
(三)測試結(jié)果
啟動服務器,顯示首頁
單擊【跳轉(zhuǎn)到登錄頁面】超鏈接
輸入正確的用戶名和密碼(易烊千璽:123456)
單擊【登錄】按鈕,跳轉(zhuǎn)到登錄成功頁面
返回登錄頁面,輸入錯誤的用戶名或密碼
單擊【登錄】按鈕,跳轉(zhuǎn)到登錄失敗頁面
二、JSP+Servlet方式實現(xiàn)用戶登錄功能
(一)實現(xiàn)思路
登錄頁面login.jsp,輸入用戶名和密碼后,跳轉(zhuǎn)到登錄處理程序LoginServlet進行業(yè)務邏輯處理,登錄成功,跳轉(zhuǎn)到登錄成功頁面success.jsp,否則跳轉(zhuǎn)到登錄失敗頁面failure.jsp。
(二)實現(xiàn)步驟
1、創(chuàng)建Web項目
創(chuàng)建 Java Enterprise
項目,添加 Web Application
功能
設(shè)置項目名與保存位置
單擊【Finish】按鈕
在項目結(jié)構(gòu)窗口里修改Artifact名 - LoginDemo02
編輯服務器配置,重新部署項目
切換到【Server】選項卡
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">賬號</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è)置請求對象字符編碼格式 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")%>,登錄失?。?lt;/h3> </body> </html>
6、編輯項目首頁
項目首頁 - 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方式實現(xiàn)用戶登錄功能</h1> <h3 style="text-align: center"><a href="login.jsp" rel="external nofollow" rel="external nofollow" >跳轉(zhuǎn)到登錄頁面</a></h3> </body> </html>
(三)測試結(jié)果
啟動服務器,顯示首頁
單擊【跳轉(zhuǎn)到登錄頁面】超鏈接
輸入正確的用戶名和密碼(易烊千璽:001128)
單擊【登錄】按鈕,跳轉(zhuǎn)到登錄成功頁面
返回登錄頁面,輸入錯誤的用戶名或密碼
單擊【登錄】按鈕,跳轉(zhuǎn)到登錄失敗頁面
三、JSP+Servlet+DB方式實現(xiàn)用戶登錄功能
(一)實現(xiàn)思路
總體上采用MVC架構(gòu)。登錄頁面login.jsp,輸入用戶名和密碼后,跳轉(zhuǎn)到登錄處理程序LoginServlet進行業(yè)務邏輯處理,調(diào)用服務層,服務層調(diào)用數(shù)據(jù)訪問層(DAO),連接數(shù)據(jù)庫,查詢數(shù)據(jù)庫,以此判斷是否登錄成功。登錄成功,跳轉(zhuǎn)到登錄成功頁面success.jsp,否則跳轉(zhuǎn)到登錄失敗頁面failure.jsp。MVC 是 Model、View 和 Controller 的縮寫,分別代表 Web 應用程序中的 3 種職責。
(二)實現(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項目
創(chuàng)建Java Enterprise項目,添加Web Application功能
設(shè)置項目名與保存位置
單擊【Finish】按鈕
在項目結(jié)構(gòu)窗口里修改Artifact名 - LoginDemo03
編輯服務器配置,重新部署項目
切換到【Server】選項卡
4、創(chuàng)建用戶實體類
創(chuàng)建net.xyx.bean包,然后在包里創(chuàng)建User類,跟用戶表(t_user)對應,簡稱ORM
package net.xyx.bean; import java.util.Date; /** * 功能:用戶實體類 * 作者: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ū)動程序
- 在WEB-INF目錄下創(chuàng)建lib目錄,添加數(shù)據(jù)庫驅(qū)動程序
- 將數(shù)據(jù)庫驅(qū)動程序(jar包)作為庫添加到項目
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ū)動程序 private static final String URL = "jdbc:mysql://localhost:3306/student/test?useSSL=false"; // 數(shù)據(jù)庫統(tǒng)一資源標識符 private static final String USER = "root"; // 數(shù)據(jù)庫用戶 private static final String PASSWORD = "1"; // 數(shù)據(jù)庫密碼 //私有化構(gòu)造方法,拒絕實例化 private ConnectionManager() { } /** * 獲取數(shù)據(jù)庫連接靜態(tài)方法 * * @return 數(shù)據(jù)庫連接對象 */ public static Connection getConnection() { // 定義數(shù)據(jù)庫連接 Connection conn = null; try { // 安裝數(shù)據(jù)庫驅(qū)動程序 Class.forName(DRIVER); // 獲取數(shù)據(jù)庫連接 conn = DriverManager.getConnection(URL, USER, PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } // 返回數(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(); } } } /** * 主方法:測試兩個靜態(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 用戶對象(非空:登錄成功,否則登錄失?。? */ public User login(String username, String password) { // 聲明用戶對象 User user = null; // 獲取數(shù)據(jù)庫連接 Connection conn = ConnectionManager.getConnection(); try { // 定義SQL字符串 String strSQL = "SELECT * FROM t_user WHERE username = ? AND password = ?"; // 創(chuàng)建預備語句對象 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)建用戶對象 user = new User(); // 利用當前記錄字段值來設(shè)置用戶對象屬性 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); } // 返回用戶對象 return user; } }
8、測試用戶數(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; /** * 功能:測試用戶數(shù)據(jù)訪問類 * 作者:xyx * 日期:2023年05月19日 */ public class TestUserDao { @Test public void testLogin() { String username = "無心劍"; String password = "12345"; // 創(chuàng)建用戶數(shù)據(jù)訪問對象 UserDao userDao = new UserDao(); // 調(diào)用登錄方法,返回用戶對象 User user = userDao.login(username, password); // 判斷用戶登錄是否成功 if (user != null) { // 成功 System.out.println("恭喜,用戶[" + username + "]登錄成功~"); } else { // 失敗 System.out.println("遺憾,用戶[" + username + "]登錄失敗~"); } } }
修改用戶名和密碼,再次運行程序,提示登錄失敗
四、采用MVC模式實現(xiàn)用戶注冊功能
1、創(chuàng)建Web項目
創(chuàng)建Java Enterprise
項目,添加Web Application
功能
設(shè)置項目名與保存位置
在項目結(jié)構(gòu)窗口里修改Artifact名 - register
編輯服務器配置,重新部署項目
2、創(chuàng)建內(nèi)容
首頁
注冊界面
隨后注冊成功和失敗都會彈出相應的界面
總結(jié)
到此這篇關(guān)于java Web實現(xiàn)用戶登錄功能的文章就介紹到這了,更多相關(guān)java Web用戶登錄功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA中String類與StringBuffer類的區(qū)別
這篇文章主要為大家詳細介紹了JAVA中String類與StringBuffer類的區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Springboot JPA打印SQL語句及參數(shù)的實現(xiàn)
在SpringBoot項目中調(diào)試和優(yōu)化數(shù)據(jù)庫操作是很常見的需求,本文主要介紹了Springboot JPA打印SQL語句及參數(shù)的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-06-06java基于jcifs.smb實現(xiàn)遠程發(fā)送文件到服務器
這篇文章主要介紹了java基于jcifs.smb實現(xiàn)遠程發(fā)送文件到服務器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01intellij idea修改maven配置時總是恢復默認配置的解決方法idea版本(2020.2.x)
這篇文章主要介紹了intellij idea修改maven配置時總是恢復默認配置的解決方法idea版本(2020.2.x),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08SpringMVC 上傳文件 MultipartFile 轉(zhuǎn)為 File的方法
這篇文章主要介紹了SpringMVC 上傳文件 MultipartFile 轉(zhuǎn)為 File的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02