Java實現(xiàn)注冊登錄跳轉(zhuǎn)
本文實例為大家分享了Java實現(xiàn)注冊登錄跳轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)建數(shù)據(jù)庫,創(chuàng)建一個登錄表login存儲用戶的用戶名和密碼,使用sql insert語句將注冊的信息插入到數(shù)據(jù)庫中,使用sql select語句查詢用戶名和密碼是否存在數(shù)據(jù)庫的login表中,實現(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>
注冊前端頁面
<%@ 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();
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解IntelliJ IDEA中TortoiseSVN修改服務(wù)器地址的方法
這篇文章主要介紹了詳解IntelliJ IDEA中TortoiseSVN修改服務(wù)器地址的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
微信開發(fā)準(zhǔn)備第二步 springmvc mybatis項目結(jié)構(gòu)搭建
這篇文章主要為大家詳細介紹了微信開發(fā)準(zhǔn)備第二步,springmvc和mybatis項目結(jié)構(gòu)的搭建,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Java如何使用JWT實現(xiàn)Token認(rèn)證機制
JWT(JSON Web Token)是一種用于在網(wǎng)絡(luò)上安全地傳輸信息的簡潔的、URL 安全的表示方法,本文主要介紹了Java如何使用JWT實現(xiàn)Token認(rèn)證機制,需要的可以參考下2024-10-10

