欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)注冊(cè)登錄跳轉(zhuǎn)

 更新時(shí)間:2022年06月16日 11:13:28   作者:skyiiiiiii  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)注冊(cè)登錄跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)注冊(cè)登錄跳轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)建數(shù)據(jù)庫(kù),創(chuàng)建一個(gè)登錄表login存儲(chǔ)用戶的用戶名和密碼,使用sql insert語句將注冊(cè)的信息插入到數(shù)據(jù)庫(kù)中,使用sql select語句查詢用戶名和密碼是否存在數(shù)據(jù)庫(kù)的login表中,實(shí)現(xiàn)登錄功能。

依賴

<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>

注冊(cè)前端頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
? ? <title>注冊(cè)</title>
</head>
<body>
<form method="post" action="login">
?? ?賬號(hào): <input type="text" name="u_no"><br>
? ? 密碼:<input type="password" name="u_pwd"><br>
? ? <button>注冊(cè)</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">
? ? 賬號(hào): <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("用戶名或密碼錯(cuò)誤");
? ? ? ? ? ? }
? ? ? ? } catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }

? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論