簡單的使用者登入頁面與後臺資料庫的互動
思路為:當使用者登入時,如果使用者名稱和密碼都正確的時候會進入成功頁面。在validate.jsp裡會先獲得使用者名稱,然後再資料裡查詢此使用者名稱是否已經存在,若存在則再與所對應的密碼進行對比,若都相同則返回flag為2,成功登入,若使用者名稱存在而密碼不正確,則返回flag為1,提示密碼錯誤;若在資料庫裡找不到對應的使用者名稱,則返回flag為0,提示使用者名稱錯誤。
程式碼實現部分:
login.jsp
<body>
<form action="validate.jsp" method="post">
<table>
<tr>
<td>使用者名稱:</td>
<td><input type="text" name="loginName"></td>
<td><font color="red"><%=request.getAttribute("loginNameError")==null ? "":request.getAttribute("loginNameError") %></font></td>
</tr>
<tr>
<td> 密碼:</td>
<td><input type="password" name="loginPass"></td>
<td><font color="red"><%=request.getAttribute("loginPassError")==null?"":request.getAttribute("loginPassError") %></font></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
validate.jsp
<body>
<%
String loginName=request.getParameter("loginName");
String loginPass=request.getParameter("loginPass");
User user=new User();
user.setUsername(loginName);
user.setPassword(loginPass);
LoginDao loginDao=new LoginDaoImpl();
int flag=loginDao.login(user);
if(flag==2){
session.setAttribute("loginName", loginName);
response.sendRedirect(request.getContextPath()+"/index.jsp");
}else {
if(flag==1){
request.setAttribute("loginPassError", "loginPass is error");
}else if(flag==0){
request.setAttribute("loginNameError", "loginName is error");
}
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
%>
</body>
//編寫一個介面,只定義方法,具體實現讓實現它的介面去完成。
LoginDao.java
package com.dao.dao;
import com.jdbc.jdbc.User;
public interface LoginDao {
int login(User user);
}
//實現介面的類,實現了具體的操作
LoginDaoImpl.java
package com.impls.impls;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jdbc.common.ConnectionUtils;
import com.dao.dao.LoginDao;
import com.jdbc.jdbc.User;
public class LoginDaoImpl implements LoginDao{
Connection con=null;
PreparedStatement pstm=null;
ResultSet rs=null;
public int login(User user) {
//連線資料庫
con=ConnectionUtils.getConnection();
int flag=0;
try {
//傳送sql文
String sql="select t_name,t_pass from t_user where t_name=?";
pstm=con.prepareStatement(sql);
pstm.setString(1, user.getUsername());
rs=pstm.executeQuery();
//處理結果集
while(rs.next()){
String temp=rs.getString("t_pass");
if(temp.equals(user.getPassword())){
flag=2; //使用者名稱和密碼都正確
}else{
flag=1;//密碼錯誤
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
//資源釋放(逆序釋放)
ConnectionUtils.close(rs, pstm, con);
}
return flag;
}
}
具體的公共類ConnectionUtils.java可以參考JDBC連線資料庫及具體程式碼實現
相關文章
- 簡單的網頁登入頁面網頁
- WebForm登入頁面(連線資料庫)WebORM資料庫
- 簡單的登入註冊(前端+後端+MySQL資料庫 DRuid連線池 DBUtils)前端後端MySql資料庫UI
- 一個超級簡單易懂的使用者登入網頁網頁
- HTML基礎實現簡單的註冊和登入頁面HTML
- C# 簡單的學生資訊管理系統,好看的UI介面,與資料庫互動C#UI資料庫
- Vue(二)使用Element-ui元件庫渲染後臺系統登入頁面VueUI元件
- 直播原始碼網站,新使用者登入時的註冊頁面和登入頁面原始碼網站
- 根據使用者的不同登入不同的頁面
- tkinter做一個簡單的登陸頁面(十六)
- 編寫一個簡單的flask的前後端互動的網頁(flask簡單知識的講解)Flask後端網頁
- 後臺登入提示:”登入失敗:資料庫目錄寫入許可權不足!“資料庫
- flask筆記:flask與資料庫的互動Flask筆記資料庫
- Redis資料庫4:Go與Redis的互動Redis資料庫Go
- MySQL資料庫5:Go與MySQL的互動MySql資料庫Go
- python資料庫-MySQL與python的互動(52)Python資料庫MySql
- 重定向到登入頁面後跳轉原頁面
- Tp6 資料庫管理工具,生成前後臺CRUD頁面,直接作為後臺頁面使用資料庫
- PbootCMS網站後臺登入頁面樣式怎麼修改boot網站
- java JDBC練手過程:使用者登入功能的實現—從前端到後臺(包括資料庫)JavaJDBC前端資料庫
- python與mysql資料庫互動PythonMySql資料庫
- PbootCMS後臺登入提示:”登入失敗:資料庫目錄寫入許可權不足!“boot資料庫
- PbootCMS後臺登入提示:“登入失敗:資料庫目錄寫入許可權不足!”boot資料庫
- Servlet實現、與html的簡單互動ServletHTML
- 使用Vue.js和Element-UI做一個簡單的登入頁面Vue.jsUI
- 登入頁面
- 動態頁面資料載入不全的問題
- Vue學習:實現使用者沒有登陸時,訪問後自動跳轉登入頁面Vue
- SqlSugar與資料庫互動官網SqlSugar資料庫
- 直播系統app原始碼,簡潔好看的登入頁面APP原始碼
- 簡單的資料輸入
- 快速上手 KSQL:輕鬆與資料庫互動的利器SQL資料庫
- 所有權背後的資料互動
- 筆記:前端與後臺互動筆記前端
- Http(s)與後臺互動方式HTTP
- 在登入資料庫的使用!sql資料庫SQL
- Hive 與 ElasticSearch 的資料互動HiveElasticsearch
- python與使用者互動、資料型別Python資料型別
- 頁面和應用之間的互動