HTML+CSS登錄界面,有數據庫的登錄驗證


HTML

 1 <!DOCTYPE html>
 2 <html lang="zh-CN">
 3     <head>
 4         <meta charset="UTF-8" />
 5         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 6         <title>用戶登錄</title>
 7         <link rel="stylesheet" href="CSS/login.css" />
 8     </head>
 9     <body>
10     <form name="user" action="LoginServlet" method="post">
11         <div class="login">
12             <h2>用戶登錄</h2>
13             <div class="login_box">
14                 <input type="text" name="uid" id="uid" autocomplete="off"/><label>用戶名</label>
15             </div>
16             <div class="login_box">
17                 <input type="password" name="password" id="password" autocomplete="off"/><label>密碼</label>
18             </div>
19             <a href="#" onclick="document.user.submit();return false">
20             登錄        
21             <span></span>
22             <span></span>
23             <span></span>
24             <span></span>
25             </a>
26         </div>
27         </form>
28     </body>
29 </html>

CSS

* {
    /* 初始化 清除頁面元素的內外邊距 */
    padding: 0;
    margin: 0;
    /* 盒子模型 */
    box-sizing: border-box;
}
body {
    /* 彈性布局 讓頁面元素垂直+水平居中 */
    display: flex;
    justify-content: center;
    align-items: center;
    /* 讓頁面始終占瀏覽器可視區域總高度 */
    height: 100vh;
    /* 背景漸變色 */
    background: linear-gradient(#141e30, #243b55);
}
.login {
    /* 彈性布局 讓子元素稱為彈性項目 */
    display: flex;
    /* 讓彈性項目垂直排列 原理是改變彈性盒子的主軸方向 父元素就是彈性盒子 現在改變后的主軸方向是向下了 */
    flex-direction: column;
    /* 讓彈性項目在交叉軸方向水平居中 現在主軸的方向是向下 交叉軸的方向是與主軸垂直 交叉軸的方向是向右 */
    align-items: center;
    width: 400px;
    padding: 40px;
    background-color: rgba(0, 0, 0, 0.2);
    box-shadow: 0 15px 25px rgba(0, 0, 0, 0.4);
}
.login h2 {
    color: #fff;
    margin-bottom: 30px;
}
.login .login_box {
    /* 相對定位 */
    position: relative;
    width: 100%;
}
.login .login_box input {
    /* 清除input框自帶的邊框和輪廓 */
    outline: none;
    border: none;
    width: 100%;
    padding: 10px 0;
    margin-bottom: 30px;
    color: #fff;
    font-size: 16px;
    border-bottom: 1px solid #fff;
    /* 背景顏色為透明色 */
    background-color: transparent;
}
.login .login_box label {
    position: absolute;
    top: 0;
    left: 0;
    padding: 10px 0;
    color: #fff;
    /* 這個屬性的默認值是auto 默認是這個元素可以被點擊 但是如果我們寫了none 就是這個元素不能被點擊 , 就好像它可見但是不能用 可望而不可即 */
    /* 這個就是兩者的區別 */
    pointer-events: none;
    /* 加個過渡 */
    transition: all 0.5s;
}
/* :focus 選擇器是當input獲得焦點是觸發的樣式 + 是相鄰兄弟選擇器 去找與input相鄰的兄弟label */
/* :valid 選擇器是判斷input框的內容是否合法,如果合法會執行下面的屬性代碼,不合法就不會執行,我們剛開始寫布局的時候給input框寫了required 我們刪掉看對比 當沒有required的話input框的值就會被認為一直合法,所以一直都是下方的樣式 ,但是密碼不會,密碼框內的值為空,那么這句話局不合法,required不能為空 當我們給密碼框寫點東西的時候才會執行以下代碼*/
.login .login_box input:focus + label,
.login .login_box input:valid + label {
    top: -20px;
    color: #03e9f4;
    font-size: 12px;
}
.login a {
    overflow: hidden;
    position: relative;
    padding: 10px 20px;
    color: #03e9f4;
    /* 取消a表現原有的下划線 */
    text-decoration: none;
    /* 同樣加個過渡 */
    transition: all 0.5s;
}

.login a:hover {
    color: #fff;
    border-radius: 5px;
    background-color: #03e9f4;
    box-shadow: 0 0 5px #03e9f4, 0 0 25px #03e9f4, 0 0 50px #03e9f4,
        0 0 100px #03e9f4;
}
.login a span {
    position: absolute;
}
.login a span:first-child {
    top: 0;
    left: -100%;
    width: 100%;
    height: 2px;
    /* to right 就是往右邊 下面的同理 */
    background: linear-gradient(to right, transparent, #03e9f4);
    /* 動畫 名稱 時長 linear是勻速運動 infinite是無限次運動 */
    animation: move1 1s linear infinite;
}
.login a span:nth-child(2) {
    right: 0;
    top: -100%;
    width: 2px;
    height: 100%;
    background: linear-gradient(transparent, #03e9f4);
    /* 這里多了個0.25s其實是延遲時間 */
    animation: move2 1s linear 0.25s infinite;
}
.login a span:nth-child(3) {
    right: -100%;
    bottom: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(to left, transparent, #03e9f4);
    animation: move3 1s linear 0.5s infinite;
}
.login a span:last-child {
    left: 0;
    bottom: -100%;
    width: 2px;
    height: 100%;
    background: linear-gradient(#03e9f4, transparent);
    animation: move4 1s linear 0.75s infinite;
}

@keyframes move1 {
    0% {
        left: -100%;
    }
    50%,
    100% {
        left: 100%;
    }
}
@keyframes move2 {
    0% {
        top: -100%;
    }
    50%,
    100% {
        top: 100%;
    }
}
@keyframes move3 {
    0% {
        right: -100%;
    }
    50%,
    100% {
        right: 100%;
    }
}
@keyframes move4 {
    0% {
        bottom: -100%;
    }
    50%,
    100% {
        bottom: 100%;
    }
}

LoginServlet

package servlet;

import java.io.IOException;
import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }
         
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=sdf.format(new Date());        
        String uid=String.valueOf(request.getParameter("uid"));
        String pass=String.valueOf(request.getParameter("password"));
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/family income and expenditure?&useSSL=false&serverTimezone=UTC";
            String username="root";
            String password="";
            Connection conn=DriverManager.getConnection(url,username,password);
            
            String sql="select * from user_info where uid='"+uid+"'and password='"+pass+"'";
            
            PreparedStatement ps=conn.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            if(rs.next()) {
                System.out.println(date+"  "+uid+" "+"login");
                System.out.println();
                request.setAttribute("uid", uid);
                request.getRequestDispatcher("index.html").forward(request,response);//登錄成功
            }else{             
                response.getWriter().write("<font style='color:red;font-size:35px'><div align='center'>"+
                "用戶名或密碼錯誤!5秒后自動跳轉"+"</div>"+"</font>");
                response.getWriter().write("<div align='center'>"
                +"如果沒有發生跳轉,請點"+"<a href='login.html'>"+"這里"+"</div>");                
                response.setHeader("refresh", "5;url=login.html");//登錄失敗,重新登錄
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            
        }
    }
}
Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM