首頁 > 軟體

Java實現註冊登入跳轉

2022-06-16 14:02:17

本文範例為大家分享了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();
        }

    }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com