栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

javaweb图书商城设计之用户模块(1)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javaweb图书商城设计之用户模块(1)

本文主要为大家分析了图书商城的用户模块,具体内容如下

1、用户模块的相关类创建

domain:User
dao:UserDao
service:UserDao
web.servlet:UserServlet

2、用户注册

2.1 注册流程

/jsps/user/regist.jsp -> UserServlet#regist() -> msg.jsp

2.2 注册页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




 

 注册

 
 
  
 
 
 
 

 

 
 注册
 <%--
 1. 显示errors --> 字段错误
 2. 显示异常错误
 3. 回显
 --%>

${msg }

2.3 UserServlet

User


public class User {
 
 private String uid;// 主键
 private String username;// 用户名
 private String password;// 密码
 private String email;// 邮箱
 private String code;// 激活码
 private boolean state;// 状态(已激活和未激活)

baseServlet

public class baseServlet extends HttpServlet {
 
 protected void service(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException {
  req.setCharacterEncoding("UTF-8");
  res.setContentType("text/html;charset=utf-8");
  try {
   // 例如:http://localhost:8080/demo1/xxx?m=add
   String method = req.getParameter("method");// 它是一个方法名称
   Class c = this.getClass();
   Method m = c.getMethod(method, HttpServletRequest.class,
     HttpServletResponse.class);
   String result = (String) m.invoke(this, req, res);
   if(result != null && !result.isEmpty()) {
    req.getRequestDispatcher(result).forward(req, res);
   }
  } catch (Exception e) {
   throw new ServletException(e);
  }
 }
}


UserServlet


public class UserServlet extends baseServlet {
 private UserService userService = new UserService();

 
 public String quit(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.getSession().invalidate();
  return "r:/index.jsp";
 }

 public String login(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  User form = CommonUtils.toBean(request.getParameterMap(), User.class);
  try {
   User user = userService.login(form);
   request.getSession().setAttribute("session_user", user);
   
   request.getSession().setAttribute("cart", new Cart());
   return "r:/index.jsp";
  } catch (UserException e) {
   request.setAttribute("msg", e.getMessage());
   request.setAttribute("form", form);
   return "f:/jsps/user/login.jsp";
  }
 }

 
 public String active(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  String code = request.getParameter("code");
  try {
   userService.active(code);
   request.setAttribute("msg", "恭喜,您激活成功了!请马上登录!");
  } catch (UserException e) {
   request.setAttribute("msg", e.getMessage());
  }
  return "f:/jsps/msg.jsp";
 }

 
 public String regist(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  // 封装表单数据
  User form = CommonUtils.toBean(request.getParameterMap(), User.class);
  // 补全
  form.setUid(CommonUtils.uuid());
  form.setCode(CommonUtils.uuid() + CommonUtils.uuid());
  
  Map errors = new HashMap();
  
  String username = form.getUsername();
  if(username == null || username.trim().isEmpty()) {
   errors.put("username", "用户名不能为空!");
  } else if(username.length() < 3 || username.length() > 10) {
   errors.put("username", "用户名长度必须在3~10之间!");
  }

  String password = form.getPassword();
  if(password == null || password.trim().isEmpty()) {
   errors.put("password", "密码不能为空!");
  } else if(password.length() < 3 || password.length() > 10) {
   errors.put("password", "密码长度必须在3~10之间!");
  }

  String email = form.getEmail();
  if(email == null || email.trim().isEmpty()) {
   errors.put("email", "Email不能为空!");
  } else if(!email.matches("\w+@\w+\.\w+")) {
   errors.put("email", "Email格式错误!");
  }
  
  if(errors.size() > 0) {
   // 1. 保存错误信息
   // 2. 保存表单数据
   // 3. 转发到regist.jsp
   request.setAttribute("errors", errors);
   request.setAttribute("form", form);
   return "f:/jsps/user/regist.jsp";
  }

  
  try {
   userService.regist(form);
  } catch (UserException e) {
   
   request.setAttribute("msg", e.getMessage());
   request.setAttribute("form", form);
   return "f:/jsps/user/regist.jsp";
  }

  
  // 获取配置文件内容
  Properties props = new Properties();
  props.load(this.getClass().getClassLoader()
    .getResourceAsStream("email_template.properties"));
  String host = props.getProperty("host");//获取服务器主机
  String uname = props.getProperty("uname");//获取用户名
  String pwd = props.getProperty("pwd");//获取密码
  String from = props.getProperty("from");//获取发件人
  String to = form.getEmail();//获取收件人
  String subject = props.getProperty("subject");//获取主题
  String content = props.getProperty("content");//获取邮件内容
  content = MessageFormat.format(content, form.getCode());//替换{0}

  Session session = MailUtils.createSession(host, uname, pwd);//得到session
  Mail mail = new Mail(from, to, subject, content);//创建邮件对象
  try {
   MailUtils.send(session, mail);//发邮件!
  } catch (MessagingException e) {
  }


  
  request.setAttribute("msg", "恭喜,注册成功!请马上到邮箱激活");
  return "f:/jsps/msg.jsp";
 }
}

2.4 UserService


public class UserService {
 private UserDao userDao = new UserDao();

 
 public void regist(User form) throws UserException{

  // 校验用户名
  User user = userDao.findByUsername(form.getUsername());
  if(user != null) throw new UserException("用户名已被注册!");

  // 校验email
  user = userDao.findByEmail(form.getEmail());
  if(user != null) throw new UserException("Email已被注册!");

  // 插入用户到数据库
  userDao.add(form);
 }

 
 public void active(String code) throws UserException {
  
  User user = userDao.findByCode(code);
  
  if(user == null) throw new UserException("激活码无效!");
  
  if(user.isState()) throw new UserException("您已经激活过了,不要再激活了,除非你想死!");

  
  userDao.updateState(user.getUid(), true);
 }

 
 public User login(User form) throws UserException {
  
  User user = userDao.findByUsername(form.getUsername());
  if(user == null) throw new UserException("用户名不存在!");
  if(!user.getPassword().equals(form.getPassword()))
   throw new UserException("密码错误!");
  if(!user.isState()) throw new UserException("尚未激活!");

  return user;
 }
}

2.5 UserDao


public class UserDao {
 private QueryRunner qr = new TxQueryRunner();

 
 public User findByUsername(String username) {
  try {
   String sql = "select * from tb_user where username=?";
   return qr.query(sql, new BeanHandler(User.class), username);
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 
 public User findByEmail(String email) {
  try {
   String sql = "select * from tb_user where email=?";
   return qr.query(sql, new BeanHandler(User.class), email);
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 
 public void add(User user) {
  try {
   String sql = "insert into tb_user values(?,?,?,?,?,?)";
   Object[] params = {user.getUid(), user.getUsername(), 
     user.getPassword(), user.getEmail(), user.getCode(),
     user.isState()};
   qr.update(sql, params);
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 
 public User findByCode(String code) {
  try {
   String sql = "select * from tb_user where code=?";
   return qr.query(sql, new BeanHandler(User.class), code);
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }

 
 public void updateState(String uid, boolean state) {
  try {
   String sql = "update tb_user set state=? where uid=?";
   qr.update(sql, state, uid);
  } catch(SQLException e) {
   throw new RuntimeException(e);
  }
 }
}

3、用户激活

流程:用户的邮件中 -> UserServlet#active() -> msg.jsp

4、

用户登录

流程:/jsps/user/login.jsp -> UserServlet#login() -> index.jsp

5、

用户退出

流程:top.jsp -> UserServlet#quit() -> login.jsp

quit():把session销毁!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148687.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号