Java实现注册登录跳转

2022-11-13 10:11:12 登录 注册 跳转

本文实例为大家分享了Java实现注册登录跳转的具体代码,供大家参考,具体内容如下

创建数据库,创建一个登录表login存储用户的用户名和密码,使用sql insert语句将注册的信息插入到数据库中,使用sql select语句查询用户名和密码是否存在数据库的login表中,实现登录功能。

依赖

<dependencies>
        <dependency>
            <groupId>Mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.24</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
</dependencies>

注册前端页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
</head>
<body>
<fORM method="post" action="login">
    账号: <input type="text" name="u_no"><br>
    密码:<input type="passWord" name="u_pwd"><br>
    <button>注册</button>
</form>
</body>
</html>

后端代码

import javax.servlet.ServletException;
import javax.servlet.annotation.WEBServlet;
import javax.servlet.Http.httpservlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String pwd = req.getParameter("u_pwd");
        String no = req.getParameter("u_no");
        try {
            Connection con = EmisUtils.getConnection();
            String sql="insert into login(u_no,u_password)values(?,?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,no);
            ps.setString(2,pwd);
            ps.execute();
            con.close();
            ps.close();
        } catch (Exception e){
            e.printStackTrace();
        }
        req.getRequestDispatcher("enter.jsp").forward(req,resp);
    }
}

登录页面前端名称 enter.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form method="post" action="enter">
    账号: <input type="text" name="u_no"><br>
    密码:<input type="password" name="u_pwd"><br>
    <button>登录</button>
</form>
</body>
</html>

后端页面

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet("/enter")
public class EnterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String no = req.getParameter("u_no");
        String pwd = req.getParameter("pwd");
        try {
            Connection connection = JdbcUtils.getConnection();
            String sql="select u_no,u_password From login where u_no =? and u_password=?";
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setString(1,no);
            ps.setString(2,pwd);
            ResultSet resultSet=ps.executeQuery();
            if(resultSet.next())
            {
                System.out.println("登录成功");
            }else {
                System.out.println("用户名或密码错误");
            }
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关文章