写的一个网页登录注册模板(css+js),注册成功后把账号保存到MySQL数据库,登录时从数据库查找进行验证(jsp+javabean)

首先是网页前端的设计,使用css美化(字体使用Google font,图标使用font awemome,颜色使用palettes|flat ui colors,谷歌可找到,个人觉得挺好用)

使用了简单的js进行表单验证用户名长度、密码、确认密码

写的一个网页登录注册模板(css+js),注册成功后把账号保存到MySQL数据库,登录时从数据库查找进行验证(jsp+javabean)_第1张图片

登录界面

注册界面

把前端代码写到jsp里面了,通过表单post提交到action,进行相应的业务逻辑处理操作

**main.jsp**
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    login-register
    
    
    
    


    
Login my own account
Register new account

Welcome

css样式

使用jquery中toggleClass来切换页面,login页面在left=-100%处,register页面在-200%处

*{
    font-family: "montserrat",sans-serif;
}
body{
    margin: 0;
    padding: 0;
    background: #333;
}
.login-box{
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100vh;
    background-image: linear-gradient(45deg,#9fbaa8,#31354c);
}
.login-form{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    color: white;
    text-align: center;
}
.login-form h1{
    font-family: 'Pacifico', cursive;
    font-weight: 400;
    font-size: 40px;
    margin-top: 0;
}
.text{
    display: block;
    box-sizing: border-box;
    width: 240px;
    background: #ffffff28;
    border: 1px solid white;
    padding: 10px 20px;
    color: white;
    outline: none;
    margin: 10px 0;
    border-radius: 6px;
    text-align: center;
}
.login-bt{
    font-family: 'Pacifico', cursive;
    width: 240px;
    background: #c0392b;
    border: 0;
    color: white;
    padding: 10px;
    border-radius: 6px;
    cursor: pointer;
}
.login-bt:hover{
    background: #e74c3c;
}
.hide-bt1{
    color: #000;
    position: absolute;
    top: 40px;
    right: 40px;
    cursor: pointer;
    font-size: 24px;

}
.bt{
    left: 50%;
    position: absolute;
    top: 50%;
    transform: translate(-50%,-50%);
    color: white;
    border: 2px solid;
    padding: 10px;
    cursor: pointer;
}
.register{
    left: 50%;
    position: absolute;
    top: 60%;
    transform: translate(-50%,-50%);
    color: white;
    border: 2px solid;
    padding: 10px;
    cursor: pointer;
}
.showed_login{
    left: 0%;
}

.register-box{
    position: absolute;
    top: 0;
    left: -200%;
    width: 100%;
    height: 100vh;
    background-image: linear-gradient(45deg,#9fbaa8,#31354c);
}
.register-form{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    color: white;
    text-align: center;
}
.register-form h1{
    font-family: 'Pacifico', cursive;
    font-weight: 400;
    font-size: 40px;
    margin-top: 0;
}
.register-bt{
    font-family: 'Pacifico', cursive;
    width: 240px;
    background: #c0392b;
    border: 0;
    color: white;
    padding: 10px;
    border-radius: 6px;
    cursor: pointer;
}
.register-bt:hover{
    background: #e74c3c;
}
.showed_register{
    left: 0%;
}
.hide-bt2{
    color: #000;
    position: absolute;
    top: 40px;
    right: 40px;
    cursor: pointer;
    font-size: 24px;

}

前端代码写完了便是对数据进行处理的部分

我在这里正在学习jsp,便选择了jsp+javabean

logincheck.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="dao.DaoFactory" %>
<%
    request.setCharacterEncoding("utf-8");
%>


    Title


    <%
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        boolean flag= false;
        try {
            flag = DaoFactory.getUserImplinstance().hasaccount(username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(flag){
            %>
    
    <%
        }
        else{
            %>
    
    <%
        }
    %>


registercheck.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="dao.DaoFactory" %>
<%@ page import="dao.User" %>
<%
    request.setCharacterEncoding("utf-8");
%>


    Title


    <%
        User user=new User();
        user.set_username(request.getParameter("username"));
        user.set_password(request.getParameter("password"));
        boolean flag= false;
        try {
            flag = DaoFactory.getUserImplinstance().addaccount(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(flag){
            %>
    

注册成功!

返回登录 <% }else { %> <% } %>

数据库操作使用javabean中的dao模式

首先是用户账号信息类

package dao;

public class User {
    private String username;
    private String password;
    public String get_username(){
        return this.username;
    }
    public String get_password(){
        return this.password;
    }
    public void set_username(String username){
        this.username=username;
    }
    public void set_password(String password){
        this.password=password;
    }
}

业务逻辑dao接口

package dao;
import  java.util.*;
public interface UserDao {
    public boolean hasaccount(String username,String password) throws Exception;
    public boolean addaccount(User user) throws Exception;
}

数据库连接

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBconnection {
    private static final String Driver="com.mysql.cj.jdbc.Driver";
    private static final String URl="jdbc:mysql://localhost:3306/mygame?useUnicode=true&serverTimezone=UTC&characterEncoding-UTF8";
    private static final String user="root";
    private static final String password="xxxxx";
    private Connection con=null;
    public DBconnection() throws SQLException, ClassNotFoundException {
        Class.forName(Driver);
        this.con= DriverManager.getConnection(URl,user,password);
    }
    public Connection getConnection(){
        return this.con;
    }
    public void close() throws SQLException {
        if(this.con!=null){
            this.con.close();
        }
    }
}

业务逻辑接口具体方法实现

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao{
    public Connection con=null;
    public PreparedStatement psmt=null;
    public UserDaoImpl(Connection con){
        this.con=con;
    }
    @Override
    public boolean hasaccount(String username,String password) throws Exception {
        boolean falg=false;
        String sql="select password from mygame.user where username=? limit 1";
        this.psmt=this.con.prepareStatement(sql);
        this.psmt.setString(1,username);
        ResultSet rs=this.psmt.executeQuery();
        while(rs.next()){
            String check_pass=rs.getString(1);
            if(check_pass.equals(password)) {
                falg = true;
            }
        }
        this.psmt.close();
        return falg;
    }

    @Override
    public boolean addaccount(User user) throws Exception {
        boolean flag=false;
        String sql="insert into mygame.user(username,password) values(?,?)";
        this.psmt=this.con.prepareStatement(sql);
        this.psmt.setString(1,user.get_username());
        this.psmt.setString(2,user.get_password());
        if(this.psmt.executeUpdate()>0){
            flag=true;
        }
        this.psmt.close();;
        return flag;
    }
}

数据库连接+业务逻辑操作

package dao;

import java.util.List;

public class UserService implements UserDao{
    private DBconnection dbcon=null;
    private UserDao dao=null;
    public UserService()throws Exception{
        this.dbcon=new DBconnection();
        this.dao=new UserDaoImpl(this.dbcon.getConnection());
    }
    @Override
    public boolean hasaccount(String username,String password) throws Exception {
        boolean flag=false;
        if(this.dao.hasaccount(username,password)){
            flag=true;
        }
        this.dbcon.close();
        return flag;
    }

    @Override
    public boolean addaccount(User user) throws Exception {
        boolean flag=false;
        try{
            if(this.dao.addaccount(user)){
                flag=true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            this.dbcon.close();
        }
        return flag;
    }
}

工厂类

package dao;

public class DaoFactory {
    public static UserDao getUserImplinstance()throws Exception{
        return new UserService();
    }
}

在这里插入图片描述

你可能感兴趣的:(java)