文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2015-04-14 20:51

关注

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

一、创建一个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 JSPtitle>
    <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">
    
head>
<body>
<div style="text-align: center;">
    <span style="font-family: 宋体; font-size: x-large; color: #000; ">欢迎JSPspan><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 JSPtitle>

    <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">
    

<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 JSPtitle>

    <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">
    

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);//得到连接

       
        PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where  UName = "" + user + "" and Pwd= "" + pwd + """);
        ResultSet rs = pStmt.executeQuery();
        if(rs.next()) {
            out.println("
用户:
"+rs.getString(1)+"密码:"+rs.getString(2)); out.println(""); }else{ out.println(""); } 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 JSPtitle>
    <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">
    
    <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">
    

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("");
    }else{
        PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values("" + user + "","" + pwd + "")");
        int rst = tmt.executeUpdate();
        if (rst != 0){
            out.println("");
        }else{
            out.println("");
        }
    }
%>
body>
html>

 

配置Tomcat

1)先run

 

 

2)

 

 

3)

 

 4)

 

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

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯