package com.atguigu.web;
import com.atguigu.dom.impl.UserDao;
import com.atguigu.dom.impl.UserDaoImpl;
import com.atguigu.pojo.User;
import com.atguigu.service.impl.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.WebUtils;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;
/**
* @author Aaron
* @date 2020/5/4 - 18:15
*/
public class UserServlet extends BaseServlet {
private UserDao userDao = new UserDaoImpl();
private UserService userService = new UserServiceImpl();
protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1 、获取请求的参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 调用 userService.login() 登录处理业务
User u = WebUtils.copyParamToBean(request.getParameterMap(),new User());
if(userService.login(u) == null){
//把错误信息和回显的表单项信息,保存到Request域中
request.setAttribute("msg","用户名或密码错误!");
request.setAttribute("username",request.getParameter("username"));
// 如果等于 null, 说明登录失败,跳回登录页面
request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
}else {
//登录成功,跳到登录成功页
//保存用户登录之后的信息
User user = userDao.queryUserByUsername(u.getUsername());
request.getSession().setAttribute("user",user);
request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
}
}
protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取请求参数,req.getParameter的参数是register.html表单项的name属性
String username = request.getParameter("username");
String password = request.getParameter("password");
String code = request.getParameter("code");
String email = request.getParameter("email");
// 2 、检查 验证码是否正确
//获取Session中的验证码(第一次请求注册页面时,会生成验证码,并将其保存到Session中)
String s = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
//删除Session中的验证码
request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
if(s != null && s.equalsIgnoreCase(code)){
// 3 、检查 用户名是否可用
if(userService.existsUsername(username)){
//把回写信息保存到Request域中
request.setAttribute("msg","用户名已存在!");
request.setAttribute("username",username);
request.setAttribute("email",email);
System.out.println("用户名[" + username +"]已存在");
//跳回注册页面
request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
}else {
//用户名可用,操作数据库,使其保存到数据库
User user = WebUtils.copyParamToBean(request.getParameterMap(),new User());
userService.registUser(user);
//跳转到注册成功界面
request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request,response);
}
}else {
System.out.println("验证码错误");
//把回写信息保存到Request域中
request.setAttribute("msg","验证码错误");
request.setAttribute("username",username);
request.setAttribute("email",email);
//重新跳回注册页面
request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
}
}
//用户注销登录
protected void logOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、销毁Session
request.getSession().invalidate();
//2、重定向到首页
response.sendRedirect(request.getContextPath());
}
protected void ajaxExitUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取请求的username
String username = request.getParameter("username");
//2、调用 userService.existsUsername();
Boolean isExit = userService.existsUsername(username);
// 把返回的结果封装成为 map 对象
Map<String, Object> map = new HashMap<>();
map.put("isExit", isExit);
//把map转成json字符串回传给浏览器
Gson gson = new Gson();
String json = gson.toJson(map);
response.getWriter().write(json);
}
}