javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)

javaWeb构建登录界面

  • javaWeb构建登录界面
    • 前言
    • 1.新建项目javaEE
    • 2.安装Tomcat
      • idea配置
    • 3.安装maven
      • 仓库
      • IDEA配置maven
    • 页面结构
    • 编写pom.xml文件添加mysql驱动
    • 4.编写前端页面
    • 5.编写后端servlet
      • JDBCUtil
      • LoginService
      • LoginServiceImpl
      • LoginPojo
      • LoginMapper
      • LoginMapperImpl
      • EnterForm
    • 操作

javaWeb构建登录界面

前言

自己改数据库,懂得无需多言,不懂说再多也不懂

1.新建项目javaEE

javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第1张图片

2.安装Tomcat

https://tomcat.apache.org/

javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第2张图片

idea配置

javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第3张图片
javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第4张图片

3.安装maven

https://maven.apache.org/
javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第5张图片

仓库

https://mvnrepository.com/

IDEA配置maven

javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第6张图片

javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第7张图片

其中的repository目录是我自己建的

页面结构

javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第8张图片

编写pom.xml文件添加mysql驱动


        
            mysql
            mysql-connector-java
            8.0.16
        

导入完之后点刷新
javaWeb构建登录界面(没办法应同学要求发一下,虽然很简单)_第9张图片

4.编写前端页面




    
    Login



username:
password:

5.编写后端servlet

JDBCUtil

package com.example.loginpro.utils;

import com.example.loginpro.entity.EnterForm;

import java.sql.*;

public class JDBCUtil {
    public String url;
    public String connect;
    public String username;
    public String password;
    public Connection cn;
    public Statement statement;
    public ResultSet res;

    public JDBCUtil() {
        this.url = "com.mysql.cj.jdbc.Driver";
        this.connect = "jdbc:mysql://:3306/syf?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true";
        this.username = "root";
        this.password = "syf20020816";
    }

    public void connectMysql() throws ClassNotFoundException, SQLException {
        Class.forName(this.url);
        this.cn = DriverManager.getConnection(this.connect, this.username, this.password);
    }

    public EnterForm selectOne() throws SQLException {
        String sql = "select * from enterform where id = 1";
        this.statement = cn.createStatement();
        this.res = this.statement.executeQuery(sql);
        this.res.next();
        EnterForm enterForm = new EnterForm();
        enterForm.setUsername(this.res.getString("username"));
        enterForm.setPwd(this.res.getString("pwd"));
        return enterForm;
    }

    public void release() throws SQLException {
        this.res.close();
        this.statement.close();
        this.cn.close();
    }
}

LoginService

package com.example.loginpro.service;

import com.example.loginpro.pojo.LoginPojo;

public interface loginService {
//    void login(String username,String password);
    String login(LoginPojo loginPojo);
}

LoginServiceImpl

package com.example.loginpro.service.impl;

import com.example.loginpro.entity.EnterForm;
import com.example.loginpro.mapper.impl.LoginMapperImpl;
import com.example.loginpro.pojo.LoginPojo;
import com.example.loginpro.service.loginService;


public class LoginServiceImpl implements loginService {
    @Override
    public String login(LoginPojo loginPojo) {
        String answer = "登录失败";
        //获取数据库中的用户名和密码与loginpojo进行对比
        LoginMapperImpl loginMapper = new LoginMapperImpl();
        EnterForm dataFormSQL = loginMapper.getDataFormSQL();
        if(!loginPojo.getUsername().equals(dataFormSQL.getUsername())){
            return  answer = "用户名错误";
        }else if(!loginPojo.getPassword().equals(dataFormSQL.getPwd())) {
            return  answer = "密码错误";
        }else{
            answer = "登录成功";
            return answer;
        }

    }
}

LoginPojo

package com.example.loginpro.pojo;

public class LoginPojo {
    private String username;
    private String password;

    public LoginPojo(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public LoginPojo() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "LoginPojo{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

LoginMapper

package com.example.loginpro.pojo;

public class LoginPojo {
    private String username;
    private String password;

    public LoginPojo(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public LoginPojo() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "LoginPojo{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

LoginMapperImpl

package com.example.loginpro.mapper.impl;

import com.example.loginpro.entity.EnterForm;
import com.example.loginpro.mapper.LoginMapper;
import com.example.loginpro.utils.JDBCUtil;

import java.sql.SQLException;

public class LoginMapperImpl implements LoginMapper {
    @Override
    public EnterForm getDataFormSQL() {
        EnterForm enterForm = new EnterForm();
        //连接数据库
        JDBCUtil jdbcUtil = new JDBCUtil();
        try {
            jdbcUtil.connectMysql();
            //进行搜索
             enterForm = jdbcUtil.selectOne();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                jdbcUtil.release();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return enterForm;
    }
}

EnterForm

package com.example.loginpro.entity;

public class EnterForm {
    private Long id;
    private String username;
    private String pwd;

    public EnterForm(Long id, String username, String pwd) {
        this.id = id;
        this.username = username;
        this.pwd = pwd;
    }

    public EnterForm() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "EnterForm{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}

操作

最后启动你的Tomcat访问localhost:8080/login.html

你可能感兴趣的:(笔记,Java学习,数据库,intellij-idea,maven,tomcat)