jsp jdbc写登录注册网页

在这里插入图片描述

第一步先通过jdbc驱动链接数据库

package JavaBean;

import java.sql.*;

public class DriverManager {
    public Connection getConn() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");//通过驱动名字来注册驱动
        String url = "jdbc:mysql://localhost:3306/student";
        String user = "root";
        String pwd = "root";
        Connection conn = java.sql.DriverManager.getConnection(url, user, pwd);
        return conn;
    }

    public Statement getStatement() throws SQLException, ClassNotFoundException {
        Connection conn = getConn();
        Statement st = conn.createStatement();
        return st;
    }

    public PreparedStatement getPreStatemrnt(String sql) throws SQLException, ClassNotFoundException {
        Connection conn = getConn();
        PreparedStatement prest = conn.prepareStatement(sql);
        return prest;
    }

    public boolean getQuery(String user, String upwd) throws SQLException, ClassNotFoundException {
    	if(user==null||upwd==null) {  //防止刚进页面未传参数的情况
    		return false;
    	}
        Statement st = getStatement();
        String sql = "select * from stuinfo;";
        ResultSet rs = st.executeQuery(sql);
        while (rs.next()) {
            String name = rs.getString("name");
            String pwd = rs.getString("pwd");
            if (user.equals(name)&&upwd.equals(pwd)) {
                return true;   //遍历结果集,比较用户输入与数据库中的值正确返回true
            }
        }
        return false;
    }

    public boolean doInsert(String user, String upwd) {
        String sql = "insert into stuinfo values('" + user + "','" + upwd + "')";
        try {
			Statement st = getStatement();
			st.executeUpdate(sql);
		} catch (ClassNotFoundException | SQLException e1) {
			// TODO Auto-generated catch block
			return false;
		}
        return true;
    }

    public boolean doDel(String user) {  //这个是为了注销用户准备的
        try {
            Statement st = getStatement();
            String sql = "select * from stuinfo;";
            ResultSet rs = st.executeQuery(sql);
            while(rs.next()){
                String name = rs.getString("name");
                if (name.equals(user)){
                    String str = "delete from stuinfo where name='"+user+"'";
                    System.out.println(str);
                    st.executeUpdate(str);
                        return true;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
}

在写主页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="JavaBean.DriverManager" %> <!--导包-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vab社区</title>
  <link rel="stylesheet" href="sheet.css"> <!--css样式连入-->
</head>
<body>



<div class="main">
  <header>
    <a href="index.html" class="logoline"><p class="logofont">Vab社区</p></a>
  </header>
  <div class="mid">
    <div class="one">
      <ul>
        <li class="oneli">C:\Windows\system\cmd.exe-mysql -u root -p</li>
        <li class="line animation"> Microsoft Windows [版本 10.0.19042.964]</li>
        <li class="line animation it">(c) Microsoft Corporation。保留所有权利。</li>
        <li><br></li>
        <li class="line animation it1">C:\Users\A8>mysql -u root -p</li>
        <li class="line animation it2">Enter password: ****</li>
        <li class="line animation it3">Welcome to the MySQL monitor.  Commands end with ; or \g.</li>
        <li class="line animation it4">Your MySQL connection id is 4</li>
        <li class="line animation it5">Server version: 5.5.27-log MySQL Community Server (GPL)</li>
        <li class="line animation it6"><br></li>
        <li class="line animation it7">Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.</li>
        <li><br></li>
        <li class="line animation it8">Oracle is a registered trademark of Oracle Corporation and/or its</li>
        <li class="line animation it9">affiliates. Other names may be trademarks of their respective</li>
        <li class="line animation it10">owners.</li>
        <li><br></li>
        <li class="line animation it11">Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.</li>
        <li><br></li>
        <li class="line animation it12">mysql> show databases;</li>
        <li class="line animation it13">+-------------------------------+</li>
        <li class="line animation it14">| Database&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
        <li class="line animation it15">+-------------------------------+</li>
        <li class="line animation it16">| information_schema&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
        <li class="line animation it17">| mysql&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
        <li class="line animation it18">| performance_schema&nbsp;&nbsp;&nbsp;|</li>
        <li class="line animation it19">+-------------------------------+</li>
        <li class="line animation it20">5 rows in set (0.00 sec)</li>
      </ul>
    </div>
    <div class="two">
      <ul>
        <li class="oneli1">login</li>
        <form action="#" method="POST" name="userinfo">
          >>userName: <input type="text" name="user" class="in"><br><br>
          >>userpwd:&nbsp;&nbsp; <input type="password" name="pwd" class="in"><br><br>
          <br><br><br>
          <input type="submit" value="登录" class="deng">
        </form>
        <br><br><br><br><br><br><br><br><br>
        <a href="register.jsp" class="a">注册账号</a>
      </ul>
    </div>
  </div>
<%   <!--做查询数据库操作,数据库中有相应的值,重定向到另一页面-->
  DriverManager dm = new DriverManager();
  String name = request.getParameter("user");
  String pwd = request.getParameter("pwd");
  boolean flag = dm.getQuery(name, pwd);
  if(flag){
	  response.sendRedirect("http://localhost:8080/test/register.jsp");
	 <!--没有写登录后的页面,所以那注册页面来使-->
  }
%>
</div>
</body>
</html>

编写css样式

*{
    margin: 0;
    padding: 0;
}

.main{
    width: 100%;
    height: 100%;
    position: fixed;
    background-color: black;
}
.logofont{
    color: rgb(13, 245, 44);
    font-size: xx-large;
    font-weight: 700;
    font-style: italic;
    margin-left: 20px;
}
.logoline{
    text-decoration-line: none;
}
.mid{
    width: 100%;
    height: 700px;
    z-index: 2;
}
div ul li{
    list-style: none;
    color: lawngreen;
    font-family: Arial, Helvetica, sans-serif;
    font-size: xx-small;
    
}
footer{
    text-align: center;
    display: inherit;
    color: rgb(19, 241, 48);
    margin-bottom: 10px;
}
.oneli{
    list-style: none;
    background-color: mediumorchid;
    border-bottom: mediumorchid;
    border-width: 1px;
}
.one{
    width: 450px;
    height: 300px;
    margin-left: 100px;
    margin-top: 100px;
    overflow: hidden;
    border-style: solid;
    border-width: 1px;
    border-color: magenta;
    position: fixed;
    animation: myactive 7s alternate-reverse 0 infinite normal normal;
    
}
.two{
    width: 450px;
    height: 450px;
    margin-left: 850px;
    margin-top: 100px;
    overflow: hidden;
    border-style: solid;
    border-width: 1px;
    border-color: magenta;
    position: relative;
}
.oneli1{
    padding: 0;
    list-style: none;
    background-color: mediumorchid;
    border-bottom: mediumorchid;
    border-width: 2px;
    text-align: center;
    font-size: large;
    font-weight: 500;
}
form{
    color: mediumseagreen;

}
form input{
    border-width: 0;
    background-color:black;
    background-origin: content-box;
}
.line{
    width:550px;
      border-right:5px solid rgb(47, 212, 14);    
      white-space:nowrap;   
      overflow:hidden; 
    }
.in{
    outline: none;
    color: chartreuse;
    border: none;
    caret-color:mediumorchid;
    text-align: left;
}
.deng{
    color: chartreuse;
    background-color: black;
    margin-left: 200px;
}
.a{
    margin-left: 300px;
    color: blueviolet;
}
.animation{ 
    animation:grow 10s steps(70) 1s normal both, blink 500ms steps(40) infinite normal;
}
@keyframes grow{
    from{width:0; }
    to{width:550px;/*段落长度 */   border-right-color:transparent; }
    0%{  border-right:0;  }/*光标起始位置不显示*/
    1%{ border-right:5px solid rgb(10, 228, 10); }/*光标显示*/
    99%{ border-right:5px solid rgb(27, 238, 8); }
    100%{border-right:0;}/*结束位置光标不显示*/
    }
    @keyframes blink{
    from{  border-right-color:#000}
    to{ border-right-color:transparent;/*文字透明化*/}
    }
    .it{
        animation-delay: 6s;
       }/*下一段延时执行动画*/
       .it1{
        animation-delay: 12s;
       }/*下一段延时执行动画*/
       .it2{
        animation-delay: 18s;
       }/*下一段延时执行动画*/
       .it3{
        animation-delay: 24s;
       }/*下一段延时执行动画*/
       .it4{
        animation-delay: 30s;
       }/*下一段延时执行动画*/
       .it5{
        animation-delay: 36s;
    }/*下一段延时执行动画*/
    .i6t{
        animation-delay: 42s;
       }/*下一段延时执行动画*/
       .it7{
        animation-delay: 48s;
       }/*下一段延时执行动画*/
       .it8{
        animation-delay: 54s;
       }/*下一段延时执行动画*/
       .it9{
        animation-delay: 60s;
       }/*下一段延时执行动画*/
       .it10{
        animation-delay: 66s;
       }/*下一段延时执行动画*/
       .it11{
        animation-delay: 72s;
       }/*下一段延时执行动画*/
       .it12{
        animation-delay: 72s;
       }
       .it13{
        animation-delay: 72s;
       }
       .it14{
        animation-delay: 72s;
       }
       .it15{
        animation-delay: 72s;
       }
       .it16{
        animation-delay: 72s;
       }
       .it17{
        animation-delay: 72s;
       }
       .it18{
        animation-delay: 72s;
       }
       .it19{
        animation-delay: 72s;
       }
       .it20{
        animation-delay: 72s;
       }

再对登录页面稍作改动,改为注册页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="JavaBean.DriverManager" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vab社区</title>
  <link rel="stylesheet" href="sheet.css">
</head>
<body>



<div class="main">
  <header>
    <a href="index.html" class="logoline"><p class="logofont">Vab社区</p></a>
  </header>
  <div class="mid">
    <div class="one">
      <ul>
        <li class="oneli">C:\Windows\system\cmd.exe-mysql -u root -p</li>
        <li class="line animation"> Microsoft Windows [版本 10.0.19042.964]</li>
        <li class="line animation it">(c) Microsoft Corporation。保留所有权利。</li>
        <li><br></li>
        <li class="line animation it1">C:\Users\A8>mysql -u root -p</li>
        <li class="line animation it2">Enter password: ****</li>
        <li class="line animation it3">Welcome to the MySQL monitor.  Commands end with ; or \g.</li>
        <li class="line animation it4">Your MySQL connection id is 4</li>
        <li class="line animation it5">Server version: 5.5.27-log MySQL Community Server (GPL)</li>
        <li class="line animation it6"><br></li>
        <li class="line animation it7">Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.</li>
        <li><br></li>
        <li class="line animation it8">Oracle is a registered trademark of Oracle Corporation and/or its</li>
        <li class="line animation it9">affiliates. Other names may be trademarks of their respective</li>
        <li class="line animation it10">owners.</li>
        <li><br></li>
        <li class="line animation it11">Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.</li>
        <li><br></li>
        <li class="line animation it12">mysql> show databases;</li>
        <li class="line animation it13">+-------------------------------+</li>
        <li class="line animation it14">| Database&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
        <li class="line animation it15">+-------------------------------+</li>
        <li class="line animation it16">| information_schema&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
        <li class="line animation it17">| mysql&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</li>
        <li class="line animation it18">| performance_schema&nbsp;&nbsp;&nbsp;|</li>
        <li class="line animation it19">+-------------------------------+</li>
        <li class="line animation it20">5 rows in set (0.00 sec)</li>
      </ul>
    </div>
    <div class="two">
      <ul>
        <li class="oneli1">register</li>
        <form action="#" method="POST" name="userinfo">
          >>userName: <input type="text" name="user" class="in"><br><br>
          >>userpwd:&nbsp;&nbsp; <input type="password" name="pwd" class="in"><br><br><br><br><br>
          <br><br><br>
          <input type="submit" value="注册" class="deng">
<%
  DriverManager dm = new DriverManager();
  String name = request.getParameter("user");
  String pwd = request.getParameter("pwd");
  boolean flag = dm.doInsert(name, pwd);  <!--做相应的插入数据操作-->
  if(flag){
	  out.print("<br><br><br><a href='demo01.jsp'>注册成功!返回登陆>></a>");
  }
%>
        </form>
      </ul>
    </div>
  </div>
</div>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: JDBC是Java数据库连接的缩,是Java语言中用于连接和操作数据库的API。Servlet是Java Web开发中的一种技术,用于处理HTTP请求和响应。JSP是Java Server Pages的缩,是一种动态网页技术,可以将Java代码嵌入到HTML页面中。 在注册与登录方面,可以使用JDBC连接数据库,将用户信息存储在数据库中。在Servlet中处理用户提交的注册和登录请求,通过JDBC操作数据库进行验证和存储。在JSP中可以使用Java代码获取数据库中的用户信息,并根据用户的登录状态显示不同的页面内容。 ### 回答2: JDBC(Java Database Connectivity)是一种用于在Java程序和数据库之间进行通信的API。在实现注册与登录功能时,JDBC可以很好地帮助我们进行用户信息的存储和查询。 首先,我们需要在数据库中建立一张用户表,用于存储用户的信息,包括用户名和密码等。在Java程序中,我们可以使用JDBC连接数据库,并使用SQL语句增加、修改、删除和查询用户信息。 在Servlet中,我们可以编处理HTTP请求的Servlet,并使用JDBC连接数据库,在用户注册时将新用户的信息插入到用户表中,而在用户登录时则使用JDBC进行查询,检查用户名和密码是否与数据库中的记录一致。 JSP(JavaServer Pages)则可以用来展示用户注册与登录的界面。我们可以编JSP页面,用于向用户展示注册与登录表单,并将用户输入的信息传递给Servlet进行处理。同时,我们还可以使用JSP中的EL表达式和JSTL标签库来完成动态数据的显示和页面的渲染。 总之,JDBC、Servlet和JSP三种技术的结合可以帮助我们实现用户注册与登录的功能。通过连接数据库和处理HTTP请求,我们可以便捷地存储和查询用户信息,而通过JSP的页面展示和渲染功能,我们可以让用户操作更加友好和美观。 ### 回答3: 在 Java Web 开发中,很多应用都需要用户注册与登录的功能,以保护用户的隐私信息或实现个性化服务等。 其中,JDBC数据库连接工具,可以连接 Java 应用与数据库之间的桥梁,提供对数据的读操作。而 JSP 和 Servlet 则是 Java web 应用的核心组件,分别负责页面展示和处理请求。 下面将以一个简单的用户注册登录功能为例,介绍 JSP、Servlet、JDBCWeb 开发中的应用。 1. 用户注册 用户注册需要收集用户输入的信息,将其保存到数据库中。 首先,在 JSP 页面中,我们需要使用 HTML 表单元素来收集用户信息。 ```html <form method="post" action="register"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> 邮箱:<input type="email" name="email"><br> <input type="submit" value="注册"> </form> ``` 当用户提交注册表单时,Servlet 将会处理请求,将收集到的用户信息存储到数据库中。在 Servlet 中,我们需要使用 JDBC 连接数据库并执行 SQL 语句。 ```java // 处理注册请求 @WebServlet("/register") public class RegisterServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取用户输入 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); // 连接数据库 String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; String user = "root"; String passwd = "123456"; try (Connection conn = DriverManager.getConnection(url, user, passwd); PreparedStatement stmt = conn.prepareStatement("INSERT INTO user(username, password, email) VALUES (?, ?, ?)")) { // 执行 SQL 语句,插入用户信息到数据库 stmt.setString(1, username); stmt.setString(2, password); stmt.setString(3, email); stmt.executeUpdate(); // 返回注册成功提示页面 resp.sendRedirect("register_success.jsp"); } catch (SQLException e) { e.printStackTrace(); } } } ``` 2. 用户登录 用户登录需要验证用户输入的用户名和密码是否匹配,并在匹配成功时创建会话信息。 同样在 JSP 页面中,用户需要输入用户名和密码。 ```html <form method="post" action="login"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登录"> </form> ``` 当用户提交登录表单时,Servlet 会对用户输入进行校验,验证用户信息是否匹配。同样需要使用 JDBC 连接数据库并执行 SQL 语句。 ```java // 处理登录请求 @WebServlet("/login") public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 获取用户输入 String username = req.getParameter("username"); String password = req.getParameter("password"); // 连接数据库 String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC"; String user = "root"; String passwd = "123456"; try (Connection conn = DriverManager.getConnection(url, user, passwd); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM user WHERE username = ?")) { // 执行 SQL 语句,查询用户信息 stmt.setString(1, username); ResultSet rs = stmt.executeQuery(); if (rs.next() && rs.getString("password").equals(password)) { // 验证成功,设置 Session 并跳转到主页 HttpSession session = req.getSession(); session.setAttribute("username", username); resp.sendRedirect("index.jsp"); } else { // 验证失败,返回登录页面 resp.sendRedirect("login.jsp"); } } catch (SQLException e) { e.printStackTrace(); } } } ``` 在主页中,我们可以通过会话信息获取当前用户并显示欢迎信息。 ```html <% HttpSession session = request.getSession(); String username = (String) session.getAttribute("username"); %> <p>欢迎 <%=username%> 登录</p> ``` 以上就是 JSP、Servlet、JDBCWeb 开发中实现用户注册登录的简单应用。总之,这些工具都是很实用的 Web 开发工具,可以帮助我们实现更加完善的 Web 应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

super_vab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值