JavaWeb 07_创建web项目连接MySQL实现注册登录功能

JavaWeb 07_创建web项目连接MySQL实现注册登录功能一、创建一个web项目,参照JW/01_创建web项目及部署 二、在NAVICat 里建数据库 db_01,建表tb_user ,字段UName 、Pwd 三、在web下创建一个Directory,

JavaWeb 07_创建web项目连接MySQL实现注册登录功能

一、创建一个web项目,参照JW/01_创建web项目及部署  

二、在NAVICat 里建数据库 db_01,建表tb_user ,字段UName 、Pwd

JavaWeb 07_创建web项目连接MySQL实现注册登录功能

 

 

三、在web下创建一个Directory, 设名字为JSPWorks

 1. 创建jsp文件如图

JavaWeb 07_创建web项目连接MySQL实现注册登录功能

 

 代码如下:

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="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="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 == "请输入用户名") 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="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="JSPWorks/shouye.jsp";</script>");
        }else{
            out.println("<script language="javascript">alert("用户登录失败!将返回首页!");window.location.href="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="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 == "输入16个字符以内") this.value ="""></td>
            </tr>
            <tr>
                <th>输入密码:</th>
                <td><input type="text" name="password" value="输入20个字符以内" maxlength = "20" onfocus = "if(this.value == "输入20个字符以内") this.value ="""></td>
            </tr>
            <tr>
                <th>确认密码:</th>
                <td><input type="text" name="newword" value="重新输入密码" maxlength = "20" onfocus = "if(this.value == "重新输入密码") 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="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="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="JSPWorks/shouye.jsp";</script>");
        }else{
            out.println("<script language="javascript">alert("用户注册失败!");window.location.href="JSPWorks/register.jsp";</script>");
        }
    }
%>
</body>
</html>

 

2. 配置Tomcat

1)先run

JavaWeb 07_创建web项目连接MySQL实现注册登录功能

 

 

2)

JavaWeb 07_创建web项目连接MySQL实现注册登录功能

 

 

3)

JavaWeb 07_创建web项目连接MySQL实现注册登录功能

 

 4)

JavaWeb 07_创建web项目连接MySQL实现注册登录功能

 

原文地址:https://www.cnblogs.com/oyww-2027/archive/2022/03/14/15308114.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/5438.html

(0)
上一篇 2023-05-09
下一篇 2023-05-09

相关推荐

  • MongoDB 聚合表达式与阶段

    MongoDB 聚合表达式与阶段聚合表达式 1. 字段路径表达式 $field $field.subfield ###2. 系统变量表达式 $$variable ###3. 常量表达式 # $literal 用于表示常量,即此处的…

    2023-02-15
    145
  • Python调用基础入门

    Python调用基础入门Python语言是一种高级编程语言,它简单易学、语法优雅、能够快速开发高效率的应用程序,尤其在数据科学领域异军突起。在使用Python编写应用程序时,会经常用到一些外部库或模块,而Python调用也是其中非常重要的一部分。本篇文章主要对Python调用的基础入门进行详细阐述。

    2024-06-16
    53
  • Python代码把妹神器:创意Python计算机科学搭讪语句合集

    Python代码把妹神器:创意Python计算机科学搭讪语句合集相信很多人都不擅长搭讪,尤其是在计算机科学与技术领域的男生更是如此。这时,Python就成了一个让男生们摆脱尴尬的神器。

    2023-12-12
    114
  • 技术分享 | Xtrabackup 备份中 Xtrabackup_binlog_info 文件记录的 GTID 信息是否准确?

    技术分享 | Xtrabackup 备份中 Xtrabackup_binlog_info 文件记录的 GTID 信息是否准确?作者:何政 本文来源:原创投稿 *原创内容未经授权不得随意使用,转载请联系小编并注明来源。 Xtrabackup 是由 percona 开源的免费数据库热备份软件,它能对 InnoDB 和 Xtra…

    2023-02-04
    128
  • 以 Python 换行为中心的标题

    以 Python 换行为中心的标题Python 是一种很有用的编程语言,它有着十分广泛的应用领域,而且便于学习和使用。Python 中有很多有趣的特性,其中之一就是换行。Python 语言中的换行比较灵活,可以在不同的场景下使用,有着相应的运用和实践。在本文中,我们将从多个方面介绍 Python 中的换行,以及它们的具体使用和实现。

    2024-06-10
    55
  • 使用Python删除字符串中指定字符的方法

    使用Python删除字符串中指定字符的方法在Python的开发中,我们难免会遇到需要删除字符串中指定字符的情况。这时,我们有多种方法来实现这个任务。本文将介绍使用Python删除字符串中指定字符的方法,希望能够为大家提供一个参考。

    2024-05-10
    66
  • Python单例模式详解

    Python单例模式详解在编写Python程序时,有时需要确保某个类只有一个实例对象,这时就需要用到单例模式。单例模式是一种常用的设计模式,可以保证一个类只有一个实例。本文将详细介绍Python单例模式,包括作用、实现方法和代码实现。

    2024-04-29
    79
  • 妈妈再也不担心我面试被Redis问得脸都绿了 (转载)[通俗易懂]

    妈妈再也不担心我面试被Redis问得脸都绿了 (转载)[通俗易懂]大概都有了,而且是从零开始的,我之前Redis博客没提到的都有,可以算补充知识。图文都有,毕竟是大佬写的:点击前往

    2023-03-05
    149

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注