簡單的登入註冊(前端+後端+MySQL資料庫 DRuid連線池 DBUtils)
簡單的登入註冊(前端+後端+MySQL資料庫 DRuid連線池 DBUtils)
結果圖
前端程式碼
- 登入頁面
<%--
Created by IntelliJ IDEA.
User: yllch
Date: 2020/10/20
Time: 16:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登入</title>
</head>
<body>
<form action="http://localhost:8080/_war_exploded/login" method="post" onsubmit="return checkAll()">
<table border="" width="700px" height="350px" align="center" style="margin-top: 90px;background-color: gray;">
<tr>
<td class="xx" colspan="2" style="text-align: center;"> 使用者登入頁面</td>
</tr>
<tr>
<td class="xx" style="text-align: center;">使用者名稱:</td>
<td class="x2">
<input type="text" name="username" id="xxx" value="" placeholder="請輸入使用者名稱6-16位字母"
oninput="checkUsername()"/>
<span id="ccc"></span>
</td>
</tr>
<tr>
<td class="xx" style="text-align: center;">密碼:</td>
<td class="x2"><input type="password" name="password" id="psd" value="" placeholder="請輸入密碼8位密碼"
oninput="checkPassord()"/><span
id="mima"></span></td>
</tr>
<tr>
<td class="xx" colspan="2" style="text-align: center;">
<input type="submit" value="登入"/>
<input type="reset" onclick="tiao()" value="註冊"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
function tiao() {
window.location='http://localhost:8080/_war_exploded/Regist.jsp';
}
function checkAll() {
return checkUsername() && checkPassord();
}
function checkUsername() {
var c = document.getElementById("xxx").value;
var regx = /^[a-zA-Z]{6,16}$/;
var f = regx.test(c);
var cc = document.getElementById("ccc");
if (f) {
cc.innerHTML = "<font color='green'>使用者名稱規則正確✔</font >"
} else {
cc.innerHTML = "<font color='red'>使用者名稱格式錯誤</font>"
}
return f;
}
function checkPassord() {
var c1 = document.getElementById("psd").value;
var regx1 = /^[a-zA-Z0-9]{8,16}$/;
var f1 = regx1.test(c1);
var cc1 = document.getElementById("mima");
if (f1) {
cc1.innerHTML = "<font color='green'>密碼格式正確✔</font >"
} else {
cc1.innerHTML = "<font color='red'>密碼格式錯誤</font>"
}
return f1;
}
</script>
- 註冊頁面
<%--
Created by IntelliJ IDEA.
User: yllch
Date: 2020/10/20
Time: 16:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>註冊</title>
</head>
<body>
<form action="http://localhost:8080/_war_exploded/regist" method="post" onsubmit="return checkAll()">
<table border="" width="700px" height="350px" align="center" style="margin-top: 90px;background-color: aquamarine;">
<tr>
<td class="xx" colspan="2" style="text-align: center;">使用者註冊頁面</td>
</tr>
<tr>
<td class="xx">使用者名稱:</td>
<td class="x2">
<input type="text" name="username" id="xxx" value="" placeholder="請輸入使用者名稱6-16位字母"
oninput="checkUsername()"/>
<span id="ccc"></span>
</td>
</tr>
<tr>
<td class="xx">密碼:</td>
<td class="x2"><input type="password" name="password" id="psd" value="" placeholder="請輸入密碼8位密碼"
oninput="checkPassord()"/><span
id="mima"></span></td>
</tr>
<tr>
<td class="xx">確認密碼:</td>
<td class="x2"><input type="password" name="password1" id="psd1" value="" placeholder="請輸入確認密碼8位密碼"
oninput="checkPassord1()"/><span
id="tishi"></span></td>
</tr>
<tr>
<td class="xx" colspan="2" style="text-align: center;">
<input type="submit" value="註冊"/>
<input type="reset" value="重置"/>
<input type="button" onclick="tiao()" value="登入"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
function checkAll() {
return checkUsername() && checkPassord() && checkPassord1();
}
function tiao() {
window.location = 'http://localhost:8080/_war_exploded/index.jsp';
}
function checkUsername() {
var c = document.getElementById("xxx").value;
var regx = /^[a-zA-Z]{6,16}$/;
var f = regx.test(c);
var cc = document.getElementById("ccc");
if (f) {
cc.innerHTML = "<font color='green'>使用者名稱規則正確✔</font >"
} else {
cc.innerHTML = "<font color='red'>使用者名稱格式錯誤</font>"
}
return f;
}
function checkPassord() {
var c1 = document.getElementById("psd").value;
var regx1 = /^[a-zA-Z0-9]{8,16}$/;
var f1 = regx1.test(c1);
var cc1 = document.getElementById("mima");
if (f1) {
cc1.innerHTML = "<font color='green'>密碼格式正確✔</font >"
} else {
cc1.innerHTML = "<font color='red'>密碼格式錯誤</font>"
}
return f1;
}
function checkPassord1() {
var c2 = document.getElementById("psd").value;
var c3 = document.getElementById("psd1").value;
var cc2 = document.getElementById("tishi");
if (c3 == c2) {
cc2.innerHTML = "<font color='green'>兩次密碼輸入一致✔</font >"
} else {
cc2.innerHTML = "<font color='red'>兩次密碼輸入不一致</font>"
}
return c3 == c2;
}
</script>
後端程式碼
- 登入
package Login;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
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.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
@WebServlet(name = "ServletLogin", value = "/login")
public class ServletLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter writer = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
QueryRunner queryRunner = new DBCon().con();
List<User> query = null;
try {
query = queryRunner.query("select * from jiaoyan", new BeanListHandler<>(User.class));
} catch (SQLException throwables) {
throwables.printStackTrace();
}
boolean i = false;
for (User user : query) {
System.out.println(user.getUsername());
System.out.println(user.getPassword());
if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
i = true;
break;
}
}
System.out.println(i);
if (i) {
writer.write("<script>alert(\"登陸成功!!\")</script>");
} else {
writer.write("<script>alert(\"登陸失敗!!\")</script>");
//response.sendRedirect("http://localhost:8080/_war_exploded/index.jsp");
}
//String sql = "select * from jiaoyan where username='" + username + "' and password='" + password + "'";
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
- 註冊
package Regist;
import Login.DBCon;
import Login.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
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.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
@WebServlet(name = "ServletRegist", value = "/regist")
public class ServletRegist extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter writer = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
QueryRunner queryRunner = new DBCon().con();
List<User> query = null;
try {
query = queryRunner.query("select * from jiaoyan", new BeanListHandler<>(User.class));
} catch (SQLException throwables) {
throwables.printStackTrace();
}
boolean i = false;
for (User user : query) {
if (username.equals(user.getUsername())) {
i = true;
break;
}
}
if (i) {
writer.write("<script>alert(\"使用者名稱已存在!!\")</script>");
//response.sendRedirect("http://localhost:8080/_war_exploded/Regist.jsp");
} else {
int update = 0;
try {
update = queryRunner.update("insert into jiaoyan values (?,?)", username, password);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (update > 0) {
writer.write("<script>alert(\"註冊成功!!\")</script>");
} else {
writer.write("<script>alert(\"註冊失敗!!\")</script>");
response.sendRedirect("http://localhost:8080/_war_exploded/Regist.jsp");
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
工具Utils程式碼
package Login;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;
public class DBCon {
public QueryRunner con() {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("druid.properties");
Properties properties = new Properties(); DataSource dataSource = null;
try {
properties.load(resourceAsStream);
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
QueryRunner queryRunner = new QueryRunner(dataSource);
return queryRunner;
}
}
User類程式碼
package Login;
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
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;
}
}
相關文章
- Python + Tkinter簡單實現註冊登入(連線本地MySQL資料庫)PythonMySql資料庫
- Druid資料庫連線池就這麼簡單UI資料庫
- Python資料庫連線池DButilsPython資料庫
- 資料庫連線池-Druid資料庫連線池原始碼解析資料庫UI原始碼
- javaweb專案(1)連線資料庫,登入註冊JavaWeb資料庫
- 聊聊資料庫連線池 Druid資料庫UI
- javaWeb 簡單註冊登入(含資料庫連線) -- (四) 使用者登入請求與響應JavaWeb資料庫
- JDBC【資料庫連線池、DbUtils框架、分頁】JDBC資料庫框架
- druid資料庫連線池的配置類UI資料庫
- Java Druid資料庫連線池+SpringJDBCJavaUI資料庫SpringJDBC
- Druid資料庫連線池使用體驗UI資料庫
- Spring Boot整合Druid資料庫連線池Spring BootUI資料庫
- 資料庫連線池_druid基本使用&工具類資料庫UI
- 【MySQL】自定義資料庫連線池和開源資料庫連線池的使用MySql資料庫
- Springboot 整合阿里資料庫連線池 druidSpring Boot阿里資料庫UI
- Java篇-DBUtils與連線池Java
- MySql資料庫連線池專題MySql資料庫
- mysql資料庫連線池配置教程MySql資料庫
- springboot專案整合druid資料庫連線池Spring BootUI資料庫
- 連線池 druidUI
- 10.註冊和登入功能實現(3)—— 註冊資料寫入資料庫資料庫
- Python 中的 MySQL 資料庫連線池PythonMySql資料庫
- 遠端連線mysql資料庫MySql資料庫
- 資料庫連線池資料庫
- 帶你進入資料庫連線池資料庫
- 《四 資料庫連線池原始碼》手寫資料庫連線池資料庫原始碼
- [轉]阿里巴巴資料庫連線池 druid配置詳解阿里資料庫UI
- Tomcat 5.5 配置 MySQL 資料庫連線池TomcatMySql資料庫
- 資料庫連線池的理解資料庫
- 資料庫連線池的使用資料庫
- WASCE的資料庫連線池資料庫
- node.js連線mysql資料庫簡單介紹Node.jsMySql資料庫
- rman連線catalog註冊資料庫問題資料庫
- 最簡單的JAVA連線資料庫Java資料庫
- MySQL 簡潔連線資料庫方式MySql資料庫
- WebForm登入頁面(連線資料庫)WebORM資料庫
- 資料庫連線池原理資料庫
- Proxool資料庫連線池資料庫