package com.itchenzn.web.servlet;

import com.itchenzn.pojo.Users;
import com.itchenzn.service.UserService;
import com.itchenzn.service.impl.UserServiceImpl;

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;

/**
 * Create by: 17467
 * Date: 2020/8/17 18:40
 */

@WebServlet("/userServlet.do")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String flag=req.getParameter("flag");
        System.out.println(flag);
        if ("addUser".equals(flag)){
            this.addUser(req,resp);
        }
    }

    /**
     * 创建添加用户的方法
     * @param req
     * @param resp
     */
    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Users users=this.createUser(req);
        UserService userService=new UserServiceImpl();
        userService.addUser(users);
        resp.sendRedirect("ok.jsp");

    }

    /**
     * 创建获取User对象的方法
     * @param req
     * @return
     */
    private Users createUser(HttpServletRequest req) {
        Users users=new Users();
        String username=req.getParameter("username");
        String usersex=req.getParameter("usersex");
        users.setUsername(username);
        users.setUsersex(usersex);
        return users;
    }
}
