關(guān)于JSP用戶登錄連接數(shù)據(jù)庫詳情
關(guān)于JSP用戶登錄連接數(shù)據(jù)庫詳情
1、首先創(chuàng)建po類
與數(shù)據(jù)庫一一對應(yīng)
lombok生成get set方法
package com.ftzlover.demo.po; import lombok.Getter; import lombok.Setter; @Getter @Setter public class User { private Integer userId; // 用戶ID private String uname; // 用戶名稱 private String upwd; // 用戶密碼 private String nick; // 用戶昵稱 private String head; // 用戶頭像 private String mood; // 用戶簽名 }
2、創(chuàng)建底層UserDao
這里就是所有創(chuàng)建好的層
3、創(chuàng)建UserService(一般都會調(diào)用UserDao)
private UserDao userDao = new UserDao();
4、寫web層UserSrevlet
注意:
- 首先需要寫@WebServlet("/user")在頂端,
- 接下來讓其調(diào)用service層private UserService userService = new UserService();
- 然后讓后讓這個(gè)類繼承 HttpServlet
public class UserServlet extends HttpServlet {
4.1 重寫方法
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
4.2創(chuàng)建vo層并在里面創(chuàng)建ResultInfo類用于封裝返回?cái)?shù)據(jù)
創(chuàng)建狀態(tài)碼code 提示信息 返回對象
@Getter @Setter public class ResultInfo<T> { private Integer code; // 狀態(tài)碼 成功=1,失敗=0 private String msg; // 提示信息 private T result; // 返回的對象(字符串、JavaBean、集合、Map等) }
5、開始從Dao開始寫
Dao層:(數(shù)據(jù)訪問層:數(shù)據(jù)庫中的增刪改查操作)通過用戶名查詢用戶對象, 返回用戶對象
獲取數(shù)據(jù)庫連接
- 定義sql語句
- 預(yù)編譯
- 設(shè)置參數(shù)
- 執(zhí)行查詢,返回結(jié)果集
- 判斷并分析結(jié)果集
- 關(guān)閉資源
package com.ftzlover.demo.dao; import com.ftzlover.demo.po.User; import com.ftzlover.demo.util.DBUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * Dao層:(數(shù)據(jù)訪問層:數(shù)據(jù)庫中的增刪改查操作) * 通過用戶名查詢用戶對象, 返回用戶對象 * 1. 獲取數(shù)據(jù)庫連接 * 2. 定義sql語句 * 3. 預(yù)編譯 * 4. 設(shè)置參數(shù) * 5. 執(zhí)行查詢,返回結(jié)果集 * 6. 判斷并分析結(jié)果集 * 7. 關(guān)閉資源 */ public class UserDao { public User queryUserByName(String userName){ //首先創(chuàng)建對象 User user = null; Connection connection = null; PreparedStatement preparedStatement = null; //預(yù)編譯對象 ResultSet resultSet = null; try { // 1. 獲取數(shù)據(jù)庫連接 connection = DBUtil.getConnetion(); // 2. 定義sql語句 String sql = "select * from tb_user where uname = ?"; // 3. 預(yù)編譯 preparedStatement = connection.prepareStatement(sql); // 4. 設(shè)置參數(shù) preparedStatement.setString(1, userName); // 5. 執(zhí)行查詢,返回結(jié)果集 resultSet = preparedStatement.executeQuery(); // 6. 判斷并分析結(jié)果集 if (resultSet.next()) { user = new User(); user.setUserId(resultSet.getInt("userId")); user.setUname(userName); user.setHead(resultSet.getString("head")); user.setMood(resultSet.getString("mood")); user.setNick(resultSet.getString("nick")); user.setUpwd(resultSet.getString("upwd")); } } catch (Exception e) { e.printStackTrace(); } finally { // 7. 關(guān)閉資源 DBUtil.close(resultSet,preparedStatement,connection); } return user; } }
6、開始寫service層
package com.ftzlover.demo.service; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.digest.DigestUtil; import com.ftzlover.demo.dao.UserDao; import com.ftzlover.demo.po.User; import com.ftzlover.demo.vo.ResultInfo; /*Service層:(業(yè)務(wù)邏輯層:參數(shù)判斷、業(yè)務(wù)邏輯處理) 1. 判斷參數(shù)是否為空 如果為空 設(shè)置ResultInfo對象的狀態(tài)碼和提示信息 返回resultInfo對象 2. 如果不為空,通過用戶名查詢用戶對象 3. 判斷用戶對象是否為空 如果為空 設(shè)置ResultInfo對象的狀態(tài)碼和提示信息 返回resultInfo對象 4. 如果用戶對象不為空,將數(shù)據(jù)庫中查詢到的用戶對象的密碼與前臺傳遞的密碼作比較 (將密碼加密后再比較) 如果密碼不正確 設(shè)置ResultInfo對象的狀態(tài)碼和提示信息 返回resultInfo對象 5. 如果密碼正確 設(shè)置ResultInfo對象的狀態(tài)碼和提示信息 6. 返回resultInfo對象 */ public class UserService { private UserDao userDao = new UserDao(); public ResultInfo<User> userLogin(String userName,String userPwd){ ResultInfo<User> resultInfo = new ResultInfo<>(); // 數(shù)據(jù)回顯:當(dāng)?shù)卿泴?shí)現(xiàn)時(shí),將登錄信息返回給頁面顯示 User u = new User(); u.setUname(userName); u.setUpwd(userPwd); // 設(shè)置到resultInfo對象中 resultInfo.setResult(u); // 1. 判斷參數(shù)是否為空 if (StrUtil.isBlank(userName) || StrUtil.isBlank(userPwd)) { // 如果為空 設(shè)置ResultInfo對象的狀態(tài)碼和提示信息 resultInfo.setCode(0); resultInfo.setMsg("用戶姓名或密碼不能為空!"); // 返回resultInfo對象 return resultInfo; } // 2. 如果不為空,通過用戶名查詢用戶對象 User user = userDao.queryUserByName(userName); // 3. 判斷用戶對象是否為空 if (user == null) { // 如果為空,設(shè)置ResultInfo對象的狀態(tài)碼和提示信息 resultInfo.setCode(0); resultInfo.setMsg("該用戶不存在!"); // 返回resultInfo對象 return resultInfo; } // 4. 如果用戶對象不為空,將數(shù)據(jù)庫中查詢到的用戶對象的密碼與前臺傳遞的密碼作比較 (將密碼加密后再比較) // 將前臺傳遞的密碼按照MD5算法的方式加密 userPwd = DigestUtil.md5Hex(userPwd); // 判斷加密后的密碼是否與數(shù)據(jù)庫中的一致 if (!userPwd.equals(user.getUpwd())) { // 如果密碼不正確 resultInfo.setCode(0); resultInfo.setMsg("用戶密碼不正確!"); return resultInfo; } resultInfo.setCode(1); resultInfo.setResult(user); return resultInfo; } }
7、編寫最后的Servelt層
7.1 用戶登陸
package com.ftzlover.demo.web; import com.ftzlover.demo.po.User; import com.ftzlover.demo.service.UserService; import com.ftzlover.demo.vo.ResultInfo; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/user") public class UserServlet extends HttpServlet { private UserService userService = new UserService(); @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 接收用戶行為 String actionName = request.getParameter("actionName"); if ("login".equals(actionName)) { // 用戶登錄 userLogin(request, response); } } /** * 用戶登錄 1. 獲取參數(shù) (姓名、密碼) 2. 調(diào)用Service層的方法,返回ResultInfo對象 3. 判斷是否登錄成功 如果失敗 將resultInfo對象設(shè)置到request作用域中 請求轉(zhuǎn)發(fā)跳轉(zhuǎn)到登錄頁面 如果成功 將用戶信息設(shè)置到session作用域中 判斷用戶是否選擇記住密碼(rem的值是1) 如果是,將用戶姓名與密碼存到cookie中,設(shè)置失效時(shí)間,并響應(yīng)給客戶端 如果否,清空原有的cookie對象 重定向跳轉(zhuǎn)到index頁面 * @param request * @param response */ private void userLogin(HttpServletRequest request, HttpServletResponse response) { // 1. 獲取參數(shù) (姓名、密碼) String userName = request.getParameter("userName"); String userPwd = request.getParameter("userPwd"); // 2. 調(diào)用Service層的方法,返回ResultInfo對象 ResultInfo<User> resultInfo = userService.userLogin(userName, userPwd); // 3. 判斷是否登錄成功 if (resultInfo.getCode() == 1) { // 如果成功 // 將用戶信息設(shè)置到session作用域中 request.getSession().setAttribute("user", resultInfo.getResult()); // 判斷用戶是否選擇記住密碼(rem的值是1) String rem = request.getParameter("rem"); // 如果是,將用戶姓名與密碼存到cookie中,設(shè)置失效時(shí)間,并響應(yīng)給客戶端 if ("1".equals(rem)) { // 得到Cookie對象 Cookie cookie = new Cookie("user",userName +"-"+userPwd); // 設(shè)置失效時(shí)間 cookie.setMaxAge(3*24*60*60); // 響應(yīng)給客戶端 response.addCookie(cookie); } else { // 如果否,清空原有的cookie對象 Cookie cookie = new Cookie("user", null); // 刪除cookie,設(shè)置maxage為0 cookie.setMaxAge(0); // 響應(yīng)給客戶端 response.addCookie(cookie); } // 重定向跳轉(zhuǎn)到index頁面 try { response.sendRedirect("index.html"); } catch (IOException e) { e.printStackTrace(); } } else { // 失敗 // 將resultInfo對象設(shè)置到request作用域中 request.setAttribute("resultInfo", resultInfo); // 請求轉(zhuǎn)發(fā)跳轉(zhuǎn)到登錄頁面 try { request.getRequestDispatcher("login.jsp").forward(request, response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
附件:util層的DBUtil
package com.ftzlover.demo.util; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class DBUtil { // 得到配置文件對象 private static Properties properties = new Properties(); static { try { // 加載配置文件(輸入流) InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties"); System.out.println("是否獲取到流對象:" + in); System.out.println("流對象:" + properties); // 通過load()方法將輸入流的內(nèi)容加載到配置文件對象中 properties.load(in); // 通過配置文件對象的getProperty()方法獲取驅(qū)動名,并加載驅(qū)動 Class.forName(properties.getProperty("jdbcName")); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnetion() { Connection connection = null; try { // 得到數(shù)據(jù)庫連接的相關(guān)信息 String dbUrl = properties.getProperty("dbUrl"); System.out.println(dbUrl); String dbName = properties.getProperty("dbName"); System.out.println(dbName); String dbPwd = properties.getProperty("dbPwd"); System.out.println(dbName); // 得到數(shù)據(jù)庫連接 connection = DriverManager.getConnection(dbUrl, dbName, dbPwd); System.out.println(connection); } catch (SQLException throwables) { throwables.printStackTrace(); } return connection; } public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) { try { // 判斷資源對象如果不為空,則關(guān)閉 if (resultSet != null) { resultSet.close(); } if (preparedStatement != null) { preparedStatement.close(); } if (connection != null) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } } }
8、示例
十分炫酷的登陸界面加完善的后臺登陸界面截圖:
數(shù)據(jù)庫代碼:新建數(shù)據(jù)庫名叫my 建表名叫tb_user
CREATE TABLE `tb_user` ( `userId` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵,自動增長', `uname` varchar(50) NOT NULL COMMENT '用戶名', `upwd` varchar(50) DEFAULT NULL COMMENT '密碼', `nick` varchar(50) DEFAULT NULL COMMENT '昵稱', `head` varchar(100) DEFAULT NULL COMMENT '頭像', `mood` varchar(500) DEFAULT NULL COMMENT '心情', PRIMARY KEY (`userId`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
到此這篇關(guān)于關(guān)于JSP用戶登錄連接數(shù)據(jù)庫詳情的文章就介紹到這了,更多相關(guān)JSP用戶登錄連接數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)
這篇文章主要介紹了基于javaweb+jsp實(shí)現(xiàn)的學(xué)生宿舍管理系統(tǒng)的示例代碼,文中的代碼介紹詳細(xì),對我們學(xué)習(xí)JSP有一定的幫助,需要的朋友可以參考一下2021-12-12Cookie的使用及保存中文并用Cookie實(shí)現(xiàn)購物車功能
Cookie是服務(wù)器存放在客戶端的一些數(shù)據(jù),比如密碼。下面為大家介紹下使用Cookie保存中文并用Cookie實(shí)現(xiàn)購物車功能,喜歡的朋友可以學(xué)習(xí)下2013-08-08通過viewport實(shí)現(xiàn)jsp頁面支持手機(jī)縮放
這篇文章主要介紹了如何通過viewport實(shí)現(xiàn)jsp頁面支持手機(jī)縮放,需要的朋友可以參考下2014-05-05JBuilder2005單元測試之創(chuàng)建測試固件
這篇文章主要介紹了JBuilder2005單元測試之創(chuàng)建測試固件2006-10-10ResourceBundle類在jsp中的國際化實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猂esourceBundle類在jsp中的國際化實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07