欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 13:01:02
本文主要为大家分析了图书商城的用户模块,具体内容如下 1、用户模块的相关类创建 domain:user dao:userdao service:userdao...

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

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" %>


<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>

 <title>注册</title>

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="this is my page">
 <meta http-equiv="content-type" content="text/html;charset=utf-8">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 </head>

 <body>
 <h1>注册</h1>
 <%--
 1. 显示errors --> 字段错误
 2. 显示异常错误
 3. 回显
 --%>
<p style="color: red; font-weight: 900">${msg }</p>
<form action="<c:url value='/userservlet'/>" method="post">
 <input type="hidden" name="method" value="regist"/>
 用户名:<input type="text" name="username" value="${form.username }"/>
 <span style="color: red; font-weight: 900">${errors.username }</span>
 <br/>
 密 码:<input type="password" name="password" value="${form.password }"/>
 <span style="color: red; font-weight: 900">${errors.password }</span>
 <br/>
 邮 箱:<input type="text" name="email" value="${form.email }"/>
 <span style="color: red; font-weight: 900">${errors.email }</span>
 <br/>
 <input type="submit" value="注册"/>
</form>
 </body>
</html>

2.3 userservlet

user

/**
 * 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 {
 /*
  * 它会根据请求中的method,来决定调用本类的哪个方法
  */
 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

/**
 * user表述层
 */
public class userservlet extends baseservlet {
 private userservice userservice = new userservice();

 /**
  * 退出功能
  * @param request
  * @param response
  * @return
  * @throws servletexception
  * @throws ioexception
  */
 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 {
  /*
   * 1. 封装表单数据到form中
   * 2. 输入校验(不写了)
   * 3. 调用service完成激活
   * > 保存错误信息、form到request,转发到login.jsp
   * 4. 保存用户信息到session中,然后重定向到index.jsp
   */
  user form = commonutils.tobean(request.getparametermap(), user.class);
  try {
   user user = userservice.login(form);
   request.getsession().setattribute("session_user", user);
   /*
    * 给用户添加一个购物车,即向session中保存一cart对象
    */
   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";
  }
 }

 /**
  * 激活功能
  * @param request
  * @param response
  * @return
  * @throws servletexception
  * @throws ioexception
  */
 public string active(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  /*
   * 1. 获取参数激活码
   * 2. 调用service方法完成激活
   * > 保存异常信息到request域,转发到msg.jsp
   * 3. 保存成功信息到request域,转发到msg.jsp
   */
  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";
 }

 /**
  * 注册功能
  * @param request
  * @param response
  * @return
  * @throws servletexception
  * @throws ioexception
  */
 public string regist(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  /*
   * 1. 封装表单数据到form对象中
   * 2. 补全:uid、code
   * 3. 输入校验
   * > 保存错误信息、form到request域,转发到regist.jsp
   * 4. 调用service方法完成注册
   * > 保存错误信息、form到request域,转发到regist.jsp
   * 5. 发邮件
   * 6. 保存成功信息转发到msg.jsp
   */
  // 封装表单数据
  user form = commonutils.tobean(request.getparametermap(), user.class);
  // 补全
  form.setuid(commonutils.uuid());
  form.setcode(commonutils.uuid() + commonutils.uuid());
  /*
   * 输入校验
   * 1. 创建一个map,用来封装错误信息,其中key为表单字段名称,值为错误信息
   */
  map<string,string> errors = new hashmap<string,string>();
  /*
   * 2. 获取form中的username、password、email进行校验
   */
  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格式错误!");
  }
  /*
   * 3. 判断是否存在错误信息
   */
  if(errors.size() > 0) {
   // 1. 保存错误信息
   // 2. 保存表单数据
   // 3. 转发到regist.jsp
   request.setattribute("errors", errors);
   request.setattribute("form", form);
   return "f:/jsps/user/regist.jsp";
  }

  /*
   * 调用service的regist()方法
   */
  try {
   userservice.regist(form);
  } catch (userexception e) {
   /*
    * 1. 保存异常信息
    * 2. 保存form
    * 3. 转发到regist.jsp
    */
   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) {
  }


  /*
   * 1. 保存成功信息
   * 2. 转发到msg.jsp
   */
  request.setattribute("msg", "恭喜,注册成功!请马上到邮箱激活");
  return "f:/jsps/msg.jsp";
 }
}

2.4 userservice

/**
 * user业务层
 */
public class userservice {
 private userdao userdao = new userdao();

 /**
  * 注册功能
  * @param form
  */
 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);
 }

 /**
  * 激活功能
  * @throws userexception 
  */
 public void active(string code) throws userexception {
  /*
   * 1. 使用code查询数据库,得到user
   */
  user user = userdao.findbycode(code);
  /*
   * 2. 如果user不存在,说明激活码错误
   */
  if(user == null) throw new userexception("激活码无效!");
  /*
   * 3. 校验用户的状态是否为未激活状态,如果已激活,说明是二次激活,抛出异常
   */
  if(user.isstate()) throw new userexception("您已经激活过了,不要再激活了,除非你想死!");

  /*
   * 4. 修改用户的状态
   */
  userdao.updatestate(user.getuid(), true);
 }

 /**
  * 登录功能
  * @param form
  * @return
  * @throws userexception 
  */
 public user login(user form) throws userexception {
  /*
   * 1. 使用username查询,得到user
   * 2. 如果user为null,抛出异常(用户名不存在)
   * 3. 比较form和user的密码,若不同,抛出异常(密码错误)
   * 4. 查看用户的状态,若为false,抛出异常(尚未激活)
   * 5. 返回user
   */
  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

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

/**
 * user持久层
 */
public class userdao {
 private queryrunner qr = new txqueryrunner();

 /**
  * 按用户名查询
  * @param username
  * @return
  */
 public user findbyusername(string username) {
  try {
   string sql = "select * from tb_user where username=?";
   return qr.query(sql, new beanhandler<user>(user.class), username);
  } catch(sqlexception e) {
   throw new runtimeexception(e);
  }
 }

 /**
  * 按email查询
  * @param email
  * @return
  */
 public user findbyemail(string email) {
  try {
   string sql = "select * from tb_user where email=?";
   return qr.query(sql, new beanhandler<user>(user.class), email);
  } catch(sqlexception e) {
   throw new runtimeexception(e);
  }
 }

 /**
  * 插入user
  * @param user
  */
 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);
  }
 }

 /**
  * 按激活码查询
  * @param code
  * @return
  */
 public user findbycode(string code) {
  try {
   string sql = "select * from tb_user where code=?";
   return qr.query(sql, new beanhandler<user>(user.class), code);
  } catch(sqlexception e) {
   throw new runtimeexception(e);
  }
 }

 /**
  * 修改指定用户的指定状态
  * @param uid
  * @param state
  */
 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

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

用户登录

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

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

用户退出

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

quit():把session销毁!

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