21xrx.com
2024-11-05 18:35:36 Tuesday
登录
文章检索 我的文章 写文章
Java实现简单登录注册功能
2023-06-19 10:41:43 深夜i     --     --
Java 登录 注册功能

文章:在开发Web应用时,登录注册功能是必不可少的模块。本文将介绍如何使用Java编写一个简单的登录注册功能。首先,我们需要一个数据库用于存储用户信息,这里我们使用MySQL数据库,并创建一个名为"users"的表,表结构如下:


create table users (

  id int primary key auto_increment,

  username varchar(50) not null,

  password varchar(50) not null

);

然后,我们使用Java JDBC连接数据库,查询用户输入的用户名和密码是否匹配。代码如下:


import java.sql.*;

public class UserDao {

  private static final String URL = "jdbc:mysql://localhost:3306/test";

  private static final String USERNAME = "root";

  private static final String PASSWORD = "root";

  public boolean checkUser(String username, String password) throws SQLException {

    try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

       PreparedStatement statement = connection.prepareStatement("select count(*) from users where username=? and password=?")) {

      statement.setString(1, username);

      statement.setString(2, password);

      ResultSet resultSet = statement.executeQuery();

      resultSet.next();

      return resultSet.getInt(1) > 0;

    }

  }

  public boolean addUser(String username, String password) throws SQLException {

    try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

       PreparedStatement statement = connection.prepareStatement("insert into users (username,password) values (?,?)")) {

      statement.setString(1, username);

      statement.setString(2, password);

      int result = statement.executeUpdate();

      return result > 0;

    }

  }

}

以上代码中,checkUser方法用于检查用户名和密码是否匹配,addUser方法用于添加新用户。接下来,我们使用Servlet处理HTTP请求,实现登录和注册功能:

登录功能:


import java.io.IOException;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/login")

public class LoginServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String username = request.getParameter("username");

    String password = request.getParameter("password");

    UserDao userDao = new UserDao();

    try {

      if (userDao.checkUser(username, password)) {

        response.getWriter().append("登陆成功!");

      } else {

        response.getWriter().append("用户名或密码错误!");

      }

    } catch (SQLException e) {

      e.printStackTrace();

    }

  }

}

注册功能:


import java.io.IOException;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/register")

public class RegisterServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String username = request.getParameter("username");

    String password = request.getParameter("password");

    UserDao userDao = new UserDao();

    try {

      if (userDao.addUser(username, password)) {

        response.getWriter().append("注册成功!");

      } else {

        response.getWriter().append("用户名已存在!");

      }

    } catch (SQLException e) {

      e.printStackTrace();

    }

  }

}

最后,在Web应用中配置以上代码,并启动应用,即可进行登录和注册功能操作。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复