一、創建一個web專案,參照JW/01_創建web專案及部署
二、在NAVICat 里建資料庫 db_01,建表tb_user ,欄位UName 、Pwd

三、在web下創建一個Directory, 設名字為JSPWorks
1. 創建jsp檔案如圖

代碼如下:
1)shouye.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="https://www.cnblogs.com/oyww-2027/p/styles.css"> --> </head> <body> <div style="text-align: center;"> <span style="font-family: 宋體; font-size: x-large; color: #000; ">歡迎JSP</span><hr> <%--<div> <img alt="" width = "600" height = "400" src=""> </div>--%> <table width = "200" border ="1" bordercolor = "#00F"> <td colspan = "2" align = "center"> <td><input type = "button" value = "登 陸" onclick = "window.open('JSPWorks/login.jsp')"></td> <td><input type = "button" value = "注 冊" onclick = "window.open('JSPWorks/register.jsp')"></td> </tr> </table> </div> </body> </html>
2)login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%! private Object Finally; %><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="https://www.cnblogs.com/oyww-2027/p/styles.css"> --> <body> <div style="text-align: center;"> <span style="font-family: 楷體; font-size: x-large; color: #000; ">登錄界面</span> <% String flag = request.getParameter("errNo"); try{ if(flag!=null) out.println("用戶名為慷訓不存在或密碼錯誤"); }catch(Exception e){ e.printStackTrace(); } %> <form action = "JSPWorks/loginCh.jsp"> <table width="300" height = "180" border="5" bordercolor="#A0A0A0"> <td colspan = "2" align = "center"> <th>賬 戶:</th> <td><input type="text" name="user" value = "請輸入用戶名" maxlength = "16" onfocus = "if(this.value =https://www.cnblogs.com/oyww-2027/p/='請輸入用戶名') this.value =''"></td> </tr> <td colspan = "2" align = "center"> <th>密 碼:</th> <td><input type="password" name="pwd" maxlength = "20"></td> </tr> <tr> <td colspan = "2" align = "center"> <td><input type="submit" name="submit" value="登 錄"></td> <td><input type="button" value="返 回" onclick = "window.open('JSPWorks/shouye.jsp')"></td> </td> </tr> </table> </form> </div> </body> </html>
3) loginCh.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="https://www.cnblogs.com/oyww-2027/p/styles.css"> --> </head> <body> <% String user = new String(request.getParameter("user").getBytes("ISO-8859-1"),"UTF-8"); String pwd = request.getParameter("pwd"); String driverClass = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/db_01"; String username = "root"; String password = "root"; try { java.lang.Class.forName(driverClass);//加載驅動 Connection conn = DriverManager.getConnection(url, username, password);//得到連接 /* if(!conn.isClosed()) { out.println("資料庫連接成功!!"); }*/ PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "' and Pwd= '" + pwd + "'"); ResultSet rs = pStmt.executeQuery(); if(rs.next()) { out.println("<br>用戶:"+rs.getString(1)+"密碼:"+rs.getString(2)); out.println("<script language='javascript'>alert('用戶登錄成功!將回傳首頁!');window.location.href='https://www.cnblogs.com/oyww-2027/p/JSPWorks/shouye.jsp';</script>"); }else{ out.println("<script language='javascript'>alert('用戶登錄失敗!將回傳首頁!');window.location.href='https://www.cnblogs.com/oyww-2027/p/JSPWorks/shouye.jsp';</script>"); } rs.close(); conn.close(); pStmt.close(); } catch(ClassNotFoundException e){ System.out.println("資料庫加載失敗!"); e.printStackTrace(); } catch(SQLException e1){ e1.printStackTrace(); } catch(Exception e2){ e2.printStackTrace(); } finally{ System.out.println("資料庫獲取成功!"); } %> </body> </html>
4) register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="https://www.cnblogs.com/oyww-2027/p/styles.css"> --> <script> function addCheck(){ var username = document.getElementById("username").value; var password = document.getElementById("password").value; var newword = document.getElementById("newword").value; if(username==""){ alert("用戶名不能為空!"); document.getElementById("username").focus(); return false; } if(password==""){ alert("密碼不能為空!"); document.getElementById("password").focus(); return false; } if(password != newword){ alert("兩次輸入密碼不相同!"); document.getElementById("newword").focus(); return false; } } function validate(){ var flag = addCheck(); if(flag == false) return false; return true; } </script> <body> <div style="text-align: center;"> <span style="font-family: 楷體; font-size: x-large; color: #000; ">注冊界面</span> <form action = "JSPWorks/checkRegister.jsp"> <table width="300" height = "180" border="5" bordercolor="#A0A0A0"> <tr> <th>用戶名:</th> <td><input type="text" name="username" value="輸入16個字符以內" maxlength = "16" onfocus = "if(this.value =https://www.cnblogs.com/oyww-2027/p/='輸入16個字符以內') this.value =''"></td> </tr> <tr> <th>輸入密碼:</th> <td><input type="text" name="password" value="輸入20個字符以內" maxlength = "20" onfocus = "if(this.value =https://www.cnblogs.com/oyww-2027/p/='輸入20個字符以內') this.value =''"></td> </tr> <tr> <th>確認密碼:</th> <td><input type="text" name="newword" value="重新輸入密碼" maxlength = "20" onfocus = "if(this.value =https://www.cnblogs.com/oyww-2027/p/='重新輸入密碼') this.value =''"></td> </tr> <tr> <td colspan = "2" align = "center"> <input type="submit" value="注 冊"> <input type="reset" value="重 置"> </td> </tr> </table> </form> </div> </body> </html>
5) checkRegister.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>檢查注冊</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="https://www.cnblogs.com/oyww-2027/p/styles.css"> --> </head> <body> <% String user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8"); String pwd = request.getParameter("password"); String driverClass = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/db_01"; String username = "root"; String password = "root"; Class.forName(driverClass);//加載驅動 Connection conn = DriverManager.getConnection(url,username,password);//得到連接 PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'"); ResultSet rs = pStmt.executeQuery(); if(rs.next()){ out.println("<script language='javascript'>alert('該用戶已存在,請重新注冊!');window.location.href='https://www.cnblogs.com/oyww-2027/p/JSPWorks/register.jsp';</script>"); }else{ PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')"); int rst = tmt.executeUpdate(); if (rst != 0){ out.println("<script language='javascript'>alert('用戶注冊成功!');window.location.href='https://www.cnblogs.com/oyww-2027/p/JSPWorks/shouye.jsp';</script>"); }else{ out.println("<script language='javascript'>alert('用戶注冊失敗!');window.location.href='https://www.cnblogs.com/oyww-2027/p/JSPWorks/register.jsp';</script>"); } } %> </body> </html>
2. 配置Tomcat
1)先run

2)

3)

4)

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/443493.html
標籤:MySQL
上一篇:揭秘位元組跳動云原生Spark History 服務 UIService
下一篇:MySQL約束
