1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| package cn.travel.web.servlet;
import cn.travel.damain.ResultInfo; import cn.travel.damain.User; import cn.travel.service.UserService; import cn.travel.service.impl.UserServiceImpl; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.beanutils.BeanUtils;
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; import java.lang.reflect.InvocationTargetException; import java.util.Map;
@WebServlet("/user/*") public class UserServlet extends BaseServlet { /** * 注册功能 * @param request * @param response * @throws ServletException * @throws IOException */ public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证码校验 String checkcode = request.getParameter("checkcode"); String checkcode_session = (String) request.getSession().getAttribute("checkcode_session");
//防止验证码复用 保证验证码只能使用一次 request.getSession().removeAttribute("checkcode_session");
//比较 if (checkcode_session == null || !checkcode_session.equalsIgnoreCase(checkcode)) { ResultInfo info = new ResultInfo(); info.setFlag(false); info.setErrorMsg("验证码错误"); //将info对象序列化为json ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(info); response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); return; }
//获取数据 String username = request.getParameter("username"); String password = request.getParameter("password"); String name = request.getParameter("name"); String birthday = request.getParameter("birthday"); String email = request.getParameter("email"); //封装对象 User user = new User(); user.setUsername(username); user.setPassword(password); user.setName(name); user.setBirthday(birthday); user.setEmail(email);
//调用service完成注册 UserService service = new UserServiceImpl(); boolean flag = service.regist(user); //响应结果 ResultInfo resultInfo = new ResultInfo(); if (flag) { //注册成功 resultInfo.setFlag(true); } else { //注册失败 resultInfo.setFlag(false); resultInfo.setErrorMsg("注册失败!"); }
//将resultInfo对象序列化为JSON,并写回客户端 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(resultInfo); //将json数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); }
/** * 登录功能 * @param request * @param response * @throws ServletException * @throws IOException */ public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取用户名和密码数据 Map<String, String[]> map = request.getParameterMap(); //分装user对象 User user = new User(); try { BeanUtils.populate(user, map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }
//调用service查询 UserService service = new UserServiceImpl(); User u = service.login(user);
//判断 ResultInfo resultInfo = new ResultInfo(); if (u == null) { //用户名密码错误 resultInfo.setFlag(false); resultInfo.setErrorMsg("用户名或密码错误!"); }
//判断用户是否激活 if (u != null && !"Y".equals(u.getStatus())) { //用户尚未激活 resultInfo.setFlag(false); resultInfo.setErrorMsg("您尚未激活,请前往邮箱激活!"); }
//登录成功的判断 if (u != null && "Y".equals(u.getStatus())) { //登录成功 resultInfo.setFlag(true); }
request.getSession().setAttribute("user", u); //响应数据 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(resultInfo); response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); }
/** * 查找单个对象 * @param request * @param response * @throws ServletException * @throws IOException */ public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Object user = request.getSession().getAttribute("user"); //将user写回客户端 ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(), user); }
/** * 退出 * @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"); }
/** * 激活 * @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) { //调用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); } } }
|