Day05JavaWeb【Cookie and Session】Integrated Case Login

Comprehensive case-login analysis

(1) Analysis
(2) Business logic
Submit login information to Sevlet
Servlet to obtain whether the user name and password already exists.
If it exists, add it, and the result is successful.
If it does not exist, return the user name or password error.
Insert picture description here

Comprehensive case-login logic

Based on TDD test-driven development, to ensure that the business logic is completely correct, it is possible to ensure that the interface displays normally on the basis.

1. Write test logic

test\java\com\wzx\TestUserService.java

public class TestUserService {
    
    
    @Test
    public void test01(){
    
    
        //1:获取参数
        String username= "jack";
        //String password= "1234";
        String password= "aaa";
        User user = new User(username,password);
        //2:调用登录
        UserService userService = new UserService();
        int code = userService.login(user);
        //3:查看结果
        System.out.println(code);
    }
}

2. Write logic
src\com\wzx\service\UserService.java

//登录与注册
public class UserService {
    
    
    public int login(User user) {
    
    
        //查找  是否存在账号与密码
        UserDao dao = new UserDao();
        int count =  dao.find(user);
        return count;
    }
}

src \ com \ wzx \ dao \ UserDao.java

public class UserDao {
    
    

    //模拟数据库数据
    private static List<User> userList = new ArrayList<User>();
    static {
    
    
        userList.add(new User("jack","aaa"));
        userList.add(new User("rose","bb"));
    }
    public int find(User user) {
    
    
        for(User u:userList){
    
    
            //存在 账号与密码 相同的用户
            if(user.getUsername().equals(u.getUsername())&&user.getPassword().equals(u.getPassword())){
    
    
                return 1;
            }
        }
        return -1;
    }
}
public class User {
    
    
    private String username;
    private String password;

Comprehensive case-webUI

web\login.jsp
edit login page, the core is a form

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<!-- 编写表单页面-->
    <form method="post" action="/taobao/login">
        username:<input name="username" type="text"/><br/>
        password:<input name="password" type="text"/><br/>
        <input  type="submit"/><br/>
    </form>
</body>
</html>

web\WEB-INF\web.xml
set login.jsp as the startup page

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--2 设置欢迎页面 -->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

src\com\wzx\web\LoginServlet.java
write page request servlet

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //1:获取参数
        //1.5 设置请求体中的中文编码utf-8
        request.setCharacterEncoding("utf-8");
    /* String username= "jack";
        //String password= "1234";
        String password= "aaa";
        User user = new User(username,password);*/
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        //1.1 创建WEB-INF/lib目录
        //1.2 添加beanutils jar
        //1.3 绑定
        //1.4 populate方法
        try {
    
    
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
    
    
            e.printStackTrace();
        } catch (InvocationTargetException e) {
    
    
            e.printStackTrace();
        }
        //2:调用登录
        UserService userService = new UserService();
        int code = userService.login(user);
        //3:查看结果
        System.out.println(code);
    }
}

  • request.setCharacterEncoding("utf-8"); Set request encoding
  • BeanUtils.populate(user,map); automatically assign the data in the form to the member variables of the object

Comprehensive case-page display after login**

  //3:查看结果
        System.out.println(code);

        if(code == 1){
    
    
            //显示主页
            response.sendRedirect(request.getContextPath()+"/home.jsp");
        }else{
    
    
            //-1
            //回到登录页面
            request.setAttribute("error_msg","账号或者密码失败");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
  • If you find that you need to put data in the request and bring it to the page, use forwarding
  • If you find that the address bar has changed and no data is required, just use redirection.
<!-- 编写表单页面-->
<font color="red">${error_msg}</font>
    <form method="post" action="/taobao/login">
        username:<input name="username" type="text"/><br/>
        password:<input name="password" type="text"/><br/>
        <input  type="submit"/><br/>
    </form>

Guess you like

Origin blog.csdn.net/u013621398/article/details/108527410