JSP实现简单的用户登录和注册

实验环境:Tomcat 9.0,MySQL 8.0,JDBC 8.0;

实验方法,利用连接池连接MySQL进行操作

首先创建一个Dynamic Web Project

1.连接池的创建

WebContent 目录下的 META_INF 目录下创建文件 context.xml记住文件的名字不能更改

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/mysql"
    auth="Container"
    type="javax.sql.DataSource"
    username="root"
    password="123456"
    maxIdle="30" 
    maxWait="10000" 
    maxActive="100"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/Duser?serverTimezone=GMT"
    logAbandoned="true" />
</Context>

2.mysql数据库的创建

create database Duser;
use Duser;
create table user(
    name varchar(30) not null,
    password varchar(30) not null,
    constraint primary key(name) //添加姓名为主键
);

4.login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录页面</title>
</head>
<body >
    <h2 align="center"><font color=red>用户登录页面</font></h2>
    <form action="success.jsp" method="post">
    <table align="center" border="1">
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>密&nbsp;&nbsp;码:</td>
            <td><input type="text" name="password"></td>
        </tr>
        <tr>
            <td><input type="submit" value="登录" name="login"></td>
            <td><input type="reset" value="重置" name="reset"></td>
        </tr>
    </table>
    <p align="center"><a href="registered.jsp" color=blue>注册用户</a></p>
    </form>

</body>
</html>

5.registered.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body align="center">
    <h2>新用户注册</h2>
    <form action="registeredSucceed.jsp" method="post">
        <table align="center">
            <tr align="right">
                 <td>请输入用户名:</td>
                <td><input type="text" name=name autofocus="autofocus"></td>
            </tr>
            <tr align="right">
                <td>请输入密码:</td>
                <td><input type="text" name=password></td>
            </tr>
            <tr align="right">
                <td>请输入确认密码:</td>
                <td><input type="text" name=refill></td>
            </tr>
        </table>
            <input type="submit" name=register value="注册" >
            <input type="reset" name=refill value="重填" >
        </form>


</body>
</html>

6.registeredSucceed.jsp

<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="javax.naming.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册成功页面</title>
</head>
<body>
<%
        Context ctx = null;
        DataSource ds = null;
        Statement stmt =null;
        ResultSet rs = null;
        Connection con = null;
        String name=request.getParameter("name").trim();//去除首尾空格
        String password=request.getParameter("password").trim();
        String refill=request.getParameter("refill").trim();
        try{
        ctx = new InitialContext();
        ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");//mysql是在创建连接池时自己创建的名字
        con = ds.getConnection();
        stmt = con.createStatement();
        if(name!=null ){
            rs=stmt.executeQuery("select * from user where name='"+name+"'");

            if(rs.next()){
                out.print("用户已经存在  "+"请<a href=\"registered.jsp\">注册</a>");
            }else{
                if(password.equals(refill)){
                     stmt.executeUpdate("insert into user values('"+name+"','"+ password + "');");
                %>
                注册成功!!!<br>
                三秒钟后自动转到登录页面!!!<br>
                如果没有跳转,请点击<a href="login.jsp">这里</a>!!!
                <span style="font-size:24px;"><meta http-equiv="refresh" content="3;URL=login.jsp"> </span>
<% 
                }else{
                out.print("密码输入不一致!!!<br>"+"重新<a href=\"registered.jsp\">注册</a>");
                }
            }
            }else {
                out.print("姓名不能为空");
            }
        }catch(Exception e){
            out.print(e);
        }finally{
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(con!=null)
                con.close();
        }
%>

</body>
</html>

7.success.jsp

<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="javax.naming.*"%>
<%request.setCharacterEncoding("utf-8"); %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>判断登录</title>
</head>
<body>
    <%
        Context ctx = null;
        DataSource ds = null;
        Statement stmt =null;
        ResultSet rs = null;
        Connection con = null;
        String name = request.getParameter("name").trim();
        String password = request.getParameter("password").trim();
        try{
            ctx = new InitialContext();
            ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");//mysql是在创建连接池时自己创建的名字  语句功能找到配置的数据库
            con = ds.getConnection();//创建数据库连接
            stmt = con.createStatement();
            rs=stmt.executeQuery("select * from user where name='"+name+"'");
            if(rs.next()){
                rs=stmt.executeQuery("select * from user where name='"+name+"' and password='"+password+"'");
                if(rs.next()){
                    out.print(name+"登录成功");
                }else{
                    out.print("密码输入错误!!!<br>"+"重新<a href=\"login.jsp\">登录</a>");
                }
            }else{
                out.print("<font color=red>"+name+"</font>用户不存在!!!<br>"+"请点击<a href=\"registered.jsp\">注册</a>");
            }
        }catch(Exception e){
            out.print(e);
        }finally{
            if(rs!=null)
                rs.close();
            if(stmt!=null)
                stmt.close();
            if(con!=null)
                con.close();
        }
    %>

</body>
</html>

另外还有登录时密码不正确的提示,以及注册时密码输入不一致的判断的功能,也可以去除注册时的空格。可以自己尝试一下这里就不做介绍了


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以加QQ(2602138376)

文章标题:JSP实现简单的用户登录和注册

文章字数:1.5k

本文作者:Zevs

发布时间:2019-09-03, 22:29:18

最后更新:2019-09-04, 18:12:11

原始链接:http://zhsh666.xyz/2019/09/03/JSP实现简单的用户登录和注册/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏

相册 图床 主题切换