欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于一个简易的html登陆界面并且连接数据库判断

程序员文章站 2022-03-23 22:10:53
关于一个简易的html登陆界面并且连接数据库判断所需要:数据库连接池druid,和tomcat服务器login.html初始化的连接数量最大连接数最大等待时间JDBC的工具类,使用Druid连接池创建登陆对象用户登录类判断登录是否成功成功界面失败界面关于数据库演示如何登录?所需要:数据库连接池druid,和tomcat服务器login.html

所需要:数据库连接池druid,和tomcat服务器

关于一个简易的html登陆界面并且连接数据库判断

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/carrent/UserLoginServlet" method="post">
    用户名:<input type="text" name="username"> <br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="登录">
</form>
</body>
</html>

druid配置文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/carrent
username=root
password=root

初始化的连接数量

initialSize=5

最大连接数

maxActive=10

最大等待时间

maxWait=3000

JDBC的工具类,使用Druid连接池

package cn.web.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * JDBC的工具类  使用Druid连接池
 */
public class JDBCUtils {
    private static DataSource ds;
    static {
        try {
            // 1.加载配置文件
            Properties properties = new Properties();
            //使用Class.loader来加载配置文件,获取字节输入流
            InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(resourceAsStream);
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2.初始化连接池
    }
    /**
    * 获取连接池对象
    */
    public static DataSource getDataSource(){
        return ds;
    }
    /**
     * 获取连接Connection对象
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}

创建登陆对象

package cn.web.person;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

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

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

用户登录类

package cn.web.login;

import cn.web.person.User;
import cn.web.utils.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class UserLogin {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    public User Login(User loginUser){
        try {
            //1.编写sql
            String sql = "select *from user where username = ? and password = ?";
            //2.调用query方法
            User user = template.queryForObject(sql,
                    new BeanPropertyRowMapper<>(User.class),
                    loginUser.getUsername(), loginUser.getPassword());
            return user;
        } catch (DataAccessException e) {
            e.printStackTrace();//记录日志
            return null;
        }
    }
}

判断登录是否成功

package cn.web.UserServlet;

import cn.web.login.UserLogin;
import cn.web.person.User;
import org.apache.commons.beanutils.BeanUtils;

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.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/UserLoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Map<String, String[]> parameterMap = req.getParameterMap();
        User loginUser = new User();
        try {
            BeanUtils.populate(loginUser,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        UserLogin userLogin = new UserLogin();
        User user = userLogin.Login(loginUser);
        //5.判断user
        if(user == null){
            //登录失败
            req.getRequestDispatcher("/UserFailServlet").forward(req,resp);
        }else {
            //登录成功
            //存储数据
            req.setAttribute("user",user);
            //转发
            req.getRequestDispatcher("/UserSuccessServlet").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

成功界面

package cn.web.UserServlet;


import cn.web.person.Admin;
import cn.web.person.User;
import cn.web.utils.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;

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;

@WebServlet("/UserSuccessServlet")
public class SuccessServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取req域共享的user对象
        User user = (User) req.getAttribute("user");
        if (user != null) {
            //给页面写一句话
            //设置编码
            resp.setContentType("text/html;charset=utf-8");
            //输出
            resp.getWriter().write("登录成功"+ user.getUsername() + ",欢迎您");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

失败界面

package cn.web.UserServlet;

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;
@WebServlet("/UserFailServlet")
public class FailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        resp.setContentType("text/html;charset=utf-8");
        //输出
        resp.getWriter().write("登录失败,用户名或密码错误");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

关于数据库

关于一个简易的html登陆界面并且连接数据库判断

演示

登录:

关于一个简易的html登陆界面并且连接数据库判断
登录成功:
关于一个简易的html登陆界面并且连接数据库判断
登录失败:
关于一个简易的html登陆界面并且连接数据库判断

如何登录?

1.首先需要启动启动tomcat服务器,把端口号8080修改为80.
并且设置虚拟目录为/carrent
2.进入:因为进入的界面为login.html故网址为:localhost/carrent/login.html,后续只需要输入数据库中存入的数据即可。若没有修改端口号则http://localhost:8080/carrent/login.html.

本文地址:https://blog.csdn.net/baldpate/article/details/109960249