|
|
|
@ -21,59 +21,71 @@ import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;
|
|
|
|
|
*/
|
|
|
|
|
public class UserServlet extends BaseServlet {
|
|
|
|
|
|
|
|
|
|
// 创建UserService的实例,使用UserServiceImpl实现类
|
|
|
|
|
private UserService userService = new UserServiceImpl();
|
|
|
|
|
|
|
|
|
|
protected void ajaxExistsusername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
//获取请求参数
|
|
|
|
|
// 获取请求参数中的用户名
|
|
|
|
|
String username = req.getParameter("username");
|
|
|
|
|
//调用userService.existsUsername()
|
|
|
|
|
boolean exitsUsername = userService.existsUsername(username);
|
|
|
|
|
//把返回的结果封装为map对象
|
|
|
|
|
Map<String,Object> resultMap = new HashMap<>();
|
|
|
|
|
resultMap.put("exitsUsername",exitsUsername);
|
|
|
|
|
|
|
|
|
|
// 调用userService.existsUsername()方法,检查用户名是否存在
|
|
|
|
|
boolean existsUsername = userService.existsUsername(username);
|
|
|
|
|
|
|
|
|
|
// 将返回的结果封装为Map对象
|
|
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
|
|
resultMap.put("existsUsername", existsUsername); // 存在性结果存入Map
|
|
|
|
|
|
|
|
|
|
// 使用Gson库将Map对象转换为JSON格式的字符串
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String json = gson.toJson(resultMap);
|
|
|
|
|
resp.getWriter().write(json);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将JSON字符串写入响应中,返回给客户端
|
|
|
|
|
resp.getWriter().write(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理登陆的功能
|
|
|
|
|
* @param req
|
|
|
|
|
* @param resp
|
|
|
|
|
*
|
|
|
|
|
* @param req HttpServletRequest 请求对象
|
|
|
|
|
* @param resp HttpServletResponse 响应对象
|
|
|
|
|
* @throws ServletException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
//1、获取请求的参数
|
|
|
|
|
// 1、获取请求的参数(用户名和密码)
|
|
|
|
|
String username = req.getParameter("username");
|
|
|
|
|
String password = req.getParameter("password");
|
|
|
|
|
User loginUser = userService.login(new User(null, username, password, null,null));
|
|
|
|
|
if(loginUser==null)
|
|
|
|
|
{
|
|
|
|
|
req.setAttribute("msg","用户名或密码错误!");
|
|
|
|
|
//req.setAttribute("username",username);
|
|
|
|
|
req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
|
|
|
|
|
|
|
|
|
|
// 2、调用userService.login()方法进行登录验证,创建User对象传递用户名和密码
|
|
|
|
|
User loginUser = userService.login(new User(null, username, password, null, null));
|
|
|
|
|
|
|
|
|
|
// 3、检查登录结果
|
|
|
|
|
if (loginUser == null) {
|
|
|
|
|
// 登录失败,设置错误信息并转发到登录页面
|
|
|
|
|
req.setAttribute("msg", "用户名或密码错误!"); // 设置错误消息
|
|
|
|
|
// req.setAttribute("username", username); // 可选:保存用户名以便回显
|
|
|
|
|
req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp); // 转发请求
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
//保存用户登录的信息到session域中
|
|
|
|
|
req.getSession().setAttribute("user",loginUser);
|
|
|
|
|
// 登录成功,将用户信息保存到session域中,以便后续使用
|
|
|
|
|
req.getSession().setAttribute("user", loginUser);
|
|
|
|
|
// 转发到登录成功页面
|
|
|
|
|
req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注销
|
|
|
|
|
* @param req
|
|
|
|
|
* @param resp
|
|
|
|
|
* @throws ServletException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* 注销用户登录
|
|
|
|
|
*
|
|
|
|
|
* @param req HttpServletRequest 请求对象,用于获取请求信息
|
|
|
|
|
* @param resp HttpServletResponse 响应对象,用于发送响应
|
|
|
|
|
* @throws ServletException 如果请求处理出现错误
|
|
|
|
|
* @throws IOException 如果输入输出操作失败
|
|
|
|
|
*/
|
|
|
|
|
protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
|
|
|
|
|
// 使当前会话失效,清除所有用户相关的session数据
|
|
|
|
|
req.getSession().invalidate();
|
|
|
|
|
|
|
|
|
|
// 重定向到应用的根路径,以返回主页或登录页面
|
|
|
|
|
resp.sendRedirect(req.getContextPath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -85,30 +97,32 @@ public class UserServlet extends BaseServlet {
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
|
|
|
|
|
//获取Session验证码
|
|
|
|
|
// 获取Session中存储的验证码
|
|
|
|
|
String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
|
|
|
|
|
//删除Session中的验证码
|
|
|
|
|
// 删除Session中的验证码,以防止重复使用
|
|
|
|
|
req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
|
|
|
|
|
//1、获取请求的参数
|
|
|
|
|
|
|
|
|
|
// 1、获取请求的参数,包括用户名、密码、确认密码、邮箱和验证码
|
|
|
|
|
String username = req.getParameter("username");
|
|
|
|
|
String password = req.getParameter("password");
|
|
|
|
|
String repwd = req.getParameter("repwd");
|
|
|
|
|
String email = req.getParameter("email");
|
|
|
|
|
String code = req.getParameter("code");
|
|
|
|
|
|
|
|
|
|
User user = (User) WebUtils.copyParamToBean(req.getParameterMap(),new User());
|
|
|
|
|
//2、检查验证码是否正确 abcde
|
|
|
|
|
if (token!=null&&token.equalsIgnoreCase(code)) {
|
|
|
|
|
//3、检查用户名是否可用
|
|
|
|
|
// 将请求参数复制到User对象中
|
|
|
|
|
User user = (User) WebUtils.copyParamToBean(req.getParameterMap(), new User());
|
|
|
|
|
|
|
|
|
|
// 2、检查验证码是否正确
|
|
|
|
|
if (token != null && token.equalsIgnoreCase(code)) {
|
|
|
|
|
// 3、检查用户名是否已被注册
|
|
|
|
|
if (userService.existsUsername(username)) {
|
|
|
|
|
//不可用 跳回注册页面
|
|
|
|
|
req.setAttribute("msg","用户名已存在!");
|
|
|
|
|
req.setAttribute("username",username);
|
|
|
|
|
req.setAttribute("email",email);
|
|
|
|
|
// 如果用户名已存在,设置错误信息并转发回注册页面
|
|
|
|
|
req.setAttribute("msg", "用户名已存在!");
|
|
|
|
|
req.setAttribute("username", username); // 保留输入的用户名以便于用户填写
|
|
|
|
|
req.setAttribute("email", email); // 保留输入的邮箱
|
|
|
|
|
req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
|
|
|
|
|
} else {
|
|
|
|
|
//可用 调用service保存到数据库 跳到注册成功页面
|
|
|
|
|
// 如果用户名可用,调用服务层方法将用户信息保存到数据库,并转发到注册成功页面
|
|
|
|
|
userService.registUser(user);
|
|
|
|
|
req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
|
|
|
|
|
}
|
|
|
|
@ -124,10 +138,19 @@ public class UserServlet extends BaseServlet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
|
User user = (User) WebUtils.copyParamToBean(req.getParameterMap(),new User());
|
|
|
|
|
// 1、将请求参数复制到User对象中
|
|
|
|
|
User user = (User) WebUtils.copyParamToBean(req.getParameterMap(), new User());
|
|
|
|
|
|
|
|
|
|
// 2、调用服务层方法更新用户信息
|
|
|
|
|
userService.updateUser(user);
|
|
|
|
|
|
|
|
|
|
// 3、根据用户ID查询更新后的用户信息
|
|
|
|
|
User user1 = userService.queryUserById(user.getId());
|
|
|
|
|
req.getSession().setAttribute("user",user1);
|
|
|
|
|
|
|
|
|
|
// 4、将更新后的用户信息存储到Session中,以便后续使用
|
|
|
|
|
req.getSession().setAttribute("user", user1);
|
|
|
|
|
|
|
|
|
|
// 5、重定向到用户信息页面,展示更新后的信息
|
|
|
|
|
resp.sendRedirect(req.getContextPath() + "/pages/user/userinfo.jsp");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|