Java實現註冊登錄跳轉

本文實例為大傢分享瞭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();
        }

    }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: