1.使用三层架构实现登录功能

2.dao层要对用用户输入的用户名的用户名进行校验

3.登录后跳转的网站的主页面,登录失败返回登录页面

项目结构图

Mysql创建数据库以及user表的创建

/*
SQLyog Ultimate v11.42 (64 bit)
MySQL - 8.0.20 : Database - userdata
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`userdata` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */;

USE `userdata`;

/*Table structure for table `user` */

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

/*Data for the table `user` */

insert  into `user`(`id`,`username`,`password`) values (1,'tom','123'),(2,'jru','456');

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

1.编写登录页面

Login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
 <style>
      body{
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  background: #34495e;
}
.box{
  width: 300px;
  padding: 40px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #191919;
  text-align: center;
}
.box h1{
  color: white;
  text-transform: uppercase;
  font-weight: 500;
}
.box input[type = "text"],.box input[type = "password"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #3498db;
  padding: 14px 10px;
  width: 200px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
}
.box input[type = "text"]:focus,.box input[type = "password"]:focus{
  width: 280px;
  border-color: #2ecc71;
}
.box input[type = "submit"]{
  border:0;
  background: none;
  display: block;
  margin: 20px auto;
  text-align: center;
  border: 2px solid #2ecc71;
  padding: 14px 40px;
  outline: none;
  color: white;
  border-radius: 24px;
  transition: 0.25s;
  cursor: poi
}
  </style>
  <body>

<form class="box" action="/userlogin/Login" method="post">
  <h1>Login</h1>
  <input type="text" name="username" placeholder="Username">
  <input type="password" name="password" placeholder="Password">
  <input type="submit" name="" value="Login">
  <a href="register.html">SIGNUP</a>
</form>

  </body>
</html>

2.编写 controller

Login.Java

package com.yin.user.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 com.yin.user.pojo.User;
import com.yin.user.service.UserService;
import com.yin.user.service.Impl.UserServiceImpl;



/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        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
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        UserService userservice =new UserServiceImpl();
        try {
            int temp =userservice.Login(user);
            if(temp>0){
                RequestDispatcher rd =request.getRequestDispatcher("/index.html");
                rd.forward(request, response);
            }
            else{
                response.sendRedirect("/userlogin/Login.html");
                
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
                
                
        
        
        
    }

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

}

3.编写service

UserService接口UserService.Java

package com.yin.user.service;

import com.yin.user.pojo.User;

public interface UserService {
    int Login(User user) throws Exception;

}

接口UserService.java实现类

UserServiceImpl.java

package com.yin.user.service.Impl;

import com.yin.user.dao.userdao;
import com.yin.user.dao.impl.UserDaoImpl;
import com.yin.user.pojo.User;
import com.yin.user.service.UserService;


public class UserServiceImpl implements UserService {

    
    @Override
    public int Login(User user) throws Exception{
      userdao userDao= new UserDaoImpl();
     int t= userDao.Login(user);
     return t;
              
       
    }
}

4.编写Dao

UserDao.java接口

package com.yin.user.dao;

import com.yin.user.pojo.User;

public interface userdao {
    
    int register(User user)throws Exception;
    int Login(User user) throws Exception;
    
}

UserDao.java接口的实现类 UserDaoImpl.java

package com.yin.user.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.yin.user.dao.userdao;
import com.yin.user.pojo.User;

public class UserDaoImpl implements userdao{
   
    public int  Login(User user) throws Exception{
        int temp=0;
        String sql ="select *from user where username=? and  password=?";
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdata", 
                "root", "yinge22");
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, user.getUserName());
        ps.setString(2, user.getPassword());    
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            temp=1;
            
            
        }
        return temp;    
    }
    
}
最后修改:2021 年 05 月 10 日 09 : 05 PM
如果觉得我的文章对你有用,请随意赞赏