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>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁老师的教学团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值