Login item

1. Project requirements

Project requirements:
Use Servlet+Druid+JdbcTemplate to realize user login function, backend is Mysql database

1. Write a static login page login.html, the page contains: name, password

2. Write a servlet that handles the login function, use the annotation version or the configuration version

3. Use the MVC three-tier architecture to write the Service layer and Dao layer, with interfaces and implementation classes

4. Use the Druid connection pool and JdbcTemplate in the Dao layer to query the Users table data in Mysql to determine whether the login is successful

5. It is required to write the druid.properties file to store the jdbc parameters, and write the JDBC tool class to obtain the data source DataSource

6. If the login is successful, jump to success.html, prompt: login is successful, you are welcome!

7. If the login fails, continue to jump to the login page.

8. The login page needs to use js or jq for form verification: if the username or password is empty, submission is not allowed, and a red text prompt will be displayed behind the corresponding input box: username is empty, or password is empty!

Second, the required packages and classes

Insert picture description here

Insert picture description here

Three, the code

Users class code:

package com.bean;

public class Users {
    
    
    private String name;
    private String psw;

    @Override
    public String toString() {
    
    
        return "Users{" +
                "name='" + name + '\'' +
                ", psw='" + psw + '\'' +
                '}';
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public String getPsw() {
    
    
        return psw;
    }

    public void setPsw(String psw) {
    
    
        this.psw = psw;
    }
}

UserDaoImpl class:

package com.dao.impl;

import com.bean.Users;
import com.dao.UserDao;
import com.utils.JdbcUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class UserDaoImpl implements UserDao {
    
    
    @Override
    public boolean login(Users users) {
    
    
        JdbcTemplate t=new JdbcTemplate(JdbcUtil.getDs());
        Users users1;
        try {
    
    
            users1 = t.queryForObject("select name,psw from users where name=? and psw=?",
                    new BeanPropertyRowMapper<Users>(Users.class), users.getName(), users.getPsw());

        }catch (Exception e){
    
    

            System.out.println("账户或者密码错误");
            return false;
        }
        return users1!=null;
        }
}

UserDao interface:

package com.dao;

import com.bean.Users;

public interface UserDao {
    
    
     boolean login(Users users);
}

UserServiceImpl class:

package com.service.impl;

import com.bean.Users;
import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
import com.service.UserService;

public class UserServiceImpl implements UserService {
    
    
   private UserDao dao=new UserDaoImpl();

    @Override
    public boolean login(Users users) {
    
    
        return dao.login(users);
    }
}

UserService interface:

package com.service;

import com.bean.Users;

public interface UserService {
    
    
    boolean login(Users users);
}

JdbcUtil class:

package com.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.beanutils.BeanUtils;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;

public class JdbcUtil {
    
    
    private static DataSource ds;
    static {
    
    
        Properties p=new Properties();
        try {
    
    
            p.load(new FileInputStream("D:\\java-two-train\\yuekao-one\\src\\druid.properties"));
            ds=new DruidDataSourceFactory().createDataSource(p);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
    public static DataSource getDs(){
    
     return ds;}
    public static <T> T mapToBean(Map map,Class<T> c){
    
    
        T t=null;
        try {
    
    
            t=c.newInstance();
            BeanUtils.populate(t,map);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return t;
    }
}

LoginServlet class:

package com.web;

import com.bean.Users;
import com.service.UserService;
import com.service.impl.UserServiceImpl;
import com.utils.JdbcUtil;

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.util.Map;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    
    
    private UserService service=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        req.setCharacterEncoding("UTF-8");
        Map<String, String[]> map = req.getParameterMap();
        Users users = JdbcUtil.mapToBean(map, Users.class);
        boolean login = service.login(users);
        if (login){
    
    
            req.getRequestDispatcher("/success.html").forward(req,resp);
        }else {
    
    
            req.getRequestDispatcher("/login1.html").forward(req,resp);
        }
    }
}

druid.properties file:

url=jdbc:mysql:///db3?characterEncoding=utf-8
username=root
password=root

login1.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-3.3.1.js"></script>
</head>
<body>
<form action="/jml/login" method="post">
    姓名: <input type="text" name="name" id="name" value=""><span id="sp"></span>
    <br>
    密码: <input type="password" name="psw" id="psw" value="">
    <br>
    <input type="button" value="提交" id="b">
    <input type="submit" value="提交" id="s">
</form>
</body>
<script>
   $('#b').click(function () {
    
    
       var name = $('#name').val();
       var psw = $('#psw').val();
       if (name==''||psw==''){
    
    
           $('#sp').css('color','red')
           $('#sp').text('用户名或密码为空')
       }else {
    
    
           $("#s").trigger("click");
       }
   })

</script>
</html>

success.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>登陆成功</h1>
</body>
</html>

Insert picture description here

to sum up

The above is the whole content of the login project, mainly using the technology and knowledge of mysql, jdbc, servlet, html, css, jq and so on.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/113137101