Day05JavaWeb【Cookie与Session】综合案例登录

综合案例-登录分析

(1)分析
(2)业务逻辑
提交登录信息给Sevlet
Servlet获取用户名密码是否已存在该用户
如果存在则添加,那么结果是成功
如果不存在则直接返回用户名或者密码出错
在这里插入图片描述

综合案例-登录逻辑

基于TDD测试驱动开发,保证业务逻辑完全正确,在基础上才有可能保证界面显示正常。

1,编写测试逻辑

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,编写逻辑
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;

综合案例-webUI

web\login.jsp
编辑登录页面,核心就是一个表单

<%@ 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
设置login.jsp为启动页面

<?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
编写页面请求的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”); 设置请求编码
  • BeanUtils.populate(user,map);自动将表单的中数据赋给对象的成员变量

综合案例-登录之后的页面显示**

  //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);
        }
  • 如果你发现,需要将数据放到请求中,带到页面,就是使用转发
  • 如果你发现,地址栏发生改变,又不需要带数据,就是使用重定向。
<!-- 编写表单页面-->
<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>

猜你喜欢

转载自blog.csdn.net/u013621398/article/details/108527410
今日推荐