@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
private UserService service = new UserServiceImpl();
/**
* 注册
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//先验证验证码:
String check = request.getParameter("check");
//获取Session中的验证码:
HttpSession session = request.getSession();
String attr_check = (String) session.getAttribute("CHECKCODE_SERVER");
session.removeAttribute("CHECKCODE_SERVER"); //保证验证码的唯一性
if( attr_check == null || !attr_check.equalsIgnoreCase(check)){
//验证码错误:
ResultInfo info = new ResultInfo();
info.setFlag(false);
info.setErrorMsg("验证码错误");
ObjectMapper mapper = new ObjectMapper();
String value = mapper.writeValueAsString(info);
//*设置响应头contentType,并返回信息
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(value);
return;
}
//1编码使用filter,获取浏览器数据
Map<String, String[]> map = request.getParameterMap();
//2.封装User对象
User user = new User();
try {
new BeanUtils().populate(user,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//3.调用service完成注册
// UserService service = new UserServiceImpl();
boolean flag = service.reginstUser(user);
//3.1创建返回信息对象:
ResultInfo info = new ResultInfo();
if(flag){
info.setFlag(true);
}else{
info.setFlag(false);
info.setErrorMsg("注册失败");
}
//4.根据service返回的信息,来提示用户
//*将信息存入json
ObjectMapper mapper = new ObjectMapper();
String value = mapper.writeValueAsString(info);
//*设置响应头contentType,并返回信息
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(value);
}
/**
* 激活
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取激活码:
String code = request.getParameter("code");
//判断验证码是否为空:
if(code != null && code.length() != 0){
//激活码存在 调用service来判断激活码
// UserService service = new UserServiceImpl();
boolean flag = service.active(code);
String msg = null;
if(flag){
//激活码正确
msg = "<a href='login.html'>激活成功,请登陆 </a>";
}else{
//激活码错误
msg = "激活失败,请与管理员联系";
}
//把数据发送会客户端
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(msg);
}
}
/**
* 登陆
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//先判断验证码是否正确:
String check = request.getParameter("check");
HttpSession session = request.getSession();
String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
if(check == null || !checkcode_server.equalsIgnoreCase(check)){
ResultInfo info = new ResultInfo();
info.setFlag(false);
info.setErrorMsg("验证码错误");
ObjectMapper mapper = new ObjectMapper();
String value = mapper.writeValueAsString(info);
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(value);
return;
}
//1.获取用户信息:
Map<String, String[]> map = request.getParameterMap();
//2.封装对象:
User user = new User();
try {
new BeanUtils().populate(user,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//3.调用service,返回一个完整的user信息
//UserService service = new UserServiceImpl();
User loginUser = service.login(user);
//3.1把用户信息存入Session中:
HttpSession userSession = request.getSession();
userSession.setAttribute("user",loginUser);
//4.判断用户是否存在
ResultInfo info = new ResultInfo();
if(loginUser == null){
info.setFlag(false);
info.setErrorMsg("登陆失败,用户名或密码错误");
}
if(loginUser != null && ("N").equals(loginUser.getStatus())){
//5.判断用户是否ji'hu激活
info.setFlag(false);
info.setErrorMsg("登陆失败,账号未激活");
}
if(loginUser != null && ("Y").equals(loginUser.getStatus())){
info.setFlag(true);
}
//6.响应信息
ObjectMapper mapper = new ObjectMapper();
String value = mapper.writeValueAsString(info);
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(value);
}
/**
* 查找单个用户
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取Session的用户信息
User user = (User) request.getSession().getAttribute("user");
//通过json将用户信息写入到客户端
ObjectMapper mapper = new ObjectMapper();
String value = mapper.writeValueAsString(user);
//响应数据
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(value);
}
/**
* 退出
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//销毁Session
request.getSession().invalidate();
//重定向到登陆页面
response.sendRedirect(request.getContextPath()+"/login.html");
}
} |
|