美文网首页
二、用户模块

二、用户模块

作者: 乔震 | 来源:发表于2017-09-02 10:13 被阅读0次


    -----------前台


    1.用户模块

                     注册            激活                登录              退出



    ---------entity

    package com.book.user.entity;

    import java.io.Serializable;

    /**

    * @author qiao

    *

    *user用户类

    */

    public class Userss implements Serializable {

    private String ids;

    private String uname;

    private String upwd;

    private String email;

    private String ucode;//激活时的uuid码

    private int state;//0是已经注册未激活;1是已经正常激活可以登录

    public String getIds() {

    return ids;

    }

    public void setIds(String ids) {

    this.ids = ids;

    }

    public String getUname() {

    return uname;

    }

    public void setUname(String uname) {

    this.uname = uname;

    }

    public String getUpwd() {

    return upwd;

    }

    public void setUpwd(String upwd) {

    this.upwd = upwd;

    }

    public String getEmail() {

    return email;

    }

    public void setEmail(String email) {

    this.email = email;

    }

    public String getUcode() {

    return ucode;

    }

    public void setUcode(String ucode) {

    this.ucode = ucode;

    }

    public int getState() {

    return state;

    }

    public void setState(int state) {

    this.state = state;

    }

    public Userss() {

    super();

    // TODO Auto-generated constructor stub

    }

    public Userss(String ids, String uname, String upwd, String email,

    String ucode, int state) {

    super();

    this.ids = ids;

    this.uname = uname;

    this.upwd = upwd;

    this.email = email;

    this.ucode = ucode;

    this.state = state;

    }

    @Override

    public String toString() {

    return "Userss [ids=" + ids + ", uname=" + uname + ", upwd=" + upwd

    + ", email=" + email + ", ucode=" + ucode + ", state=" + state

    + "]";

    }

    }

    ---------dao


    package com.book.user.dao;

    import java.sql.SQLException;

    import org.apache.commons.dbutils.QueryRunner;

    import org.apache.commons.dbutils.handlers.BeanHandler;

    import com.book.user.entity.Userss;

    import cn.itcast.jdbc.TxQueryRunner;

    /**

    * @author qiao

    *        用户操作数据库部分

    */

    public class UserDaoImpl {

    private QueryRunner qr = new TxQueryRunner();

    // 根据用户名查询用户

    public Userss findByUsername(String username) {

    try {

    String sql = "select * from userss where uname=?";

    return qr.query(sql, new BeanHandler(Userss.class),username);

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    throw new RuntimeException("出错了!findByUserName");

    }

    }

    // 根据邮箱得到一个用户

    public Userss findByEmail(String email) {

    try {

    String sql = "select * from userss where email=?";

    return qr.query(sql, new BeanHandler(Userss.class), email);

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    throw new RuntimeException("出错了,findByEmail");

    }

    }

    // 添加用户到数据库

    public void add(Userss userss) {

    try {

    String sql = "insert into userss values(?,?,?,?,?,?)";

    // 注意和问号顺序一致

    Object[] params = { userss.getIds(),   userss.getUname(),userss.getUpwd(), userss.getEmail(), userss.getUcode(),userss.getState() };

    //添加数据到数据库中

    qr.update(sql, params);

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    //准备激活

    public Userss findByCode(String code){

    try {

    String sql = "select * from userss where ucode=?";

    return qr.query(sql, new BeanHandler(Userss.class),code);

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    throw new RuntimeException("出错了,findByCode");

    }

    }

    //激活的本质

    public void updateState(String uid,int state){

    try {

    String sql="update userss set state=? where ids=?";

    qr.update(sql, state,uid);

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    --------service

    /**

    * @author qiao

    *

    *用户自定义异常类

    */

    public class UserssException extends Exception {

    public UserssException() {

    super();

    // TODO Auto-generated constructor stub

    }

    public UserssException(String message) {

    super(message);

    // TODO Auto-generated constructor stub

    }

    }

    /**

    * @author qiao

    *

    *业务逻辑类

    */

    public class UserBizImpl {

    private UserDaoImpl udao;

    public UserBizImpl() {

    udao=new UserDaoImpl();

    }

    //用户注册功能

    public void register(Userss form) throws UserssException{

    //根据姓名查询用户

    Userss userss=udao.findByUsername(form.getUname());

    if(userss!=null){

    throw new UserssException("用户已经被注册!");

    }

    userss=udao.findByEmail(form.getEmail());

    if(userss!=null){

    throw new UserssException("邮箱已经被注册!");

    }

    //表单传过来的数据放入数据库

    udao.add(form);

    }

    //激活方法

    public void active(String code) throws UserssException{

    //使用code查询用户

    Userss users = udao.findByCode(code);

    //激活码无效

    if(users==null){

    throw new UserssException("激活码无效");

    }

    if(users.getState()==1){

    throw new UserssException("你已经激活了,无须再次激活");

    }

    udao.updateState(users.getIds(), 1);

    }

    //登录

    public Userss login(Userss form) throws UserssException{

    /*

    * 1.使用username查询,得到user

    * 2.如果user为null,抛出异常(用户不存在)

    * 3.校验form和user的密码,如果不想等,则密码错误

    * 4.查询用户状态,如果为0,尚未激活

    * 5.返回user

    */

    Userss userss = udao.findByUsername(form.getUname());

    if(userss==null){

    throw new UserssException("用户不存在!");

    }

    if(!userss.getUpwd().equals(form.getUpwd())){

    throw new UserssException("密码错误");

    }

    if(userss.getState()==0){

    throw new UserssException("尚未激活");

    }

    return userss;

    }

    }

    -----servlet

    /** * 所有和userss类相关的操作都放在当前类 */

    public class UsersServlet extends BaseServlet {

    private UserBizImpl biz = new UserBizImpl();

    /**

    * 注册方法

    *  @param request

    *@param response

    * @return

    * @throws ServletException

    * @throws IOException

    */

    public String regist(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

    /** * 1.封装表单数据到form对象中 Userss form

    * 2.补全ids ucode state

    * 3.输入校验 * 如果有错误信息保存一下,去regist.jsp显示

    * 4.调用biz的注册方法

    * 5.发送邮件

    * 6.成功了保存信息到msg.jsp */

    //request.getParameter("")    //表单的name属性必须和实体类的属性一致

    Userss form=CommonUtils.toBean(request.getParameterMap(),Userss.class);form.setIds(CommonUtils.uuid());//设置uuid//激活码用64位form.setUcode(CommonUtils.uuid()+CommonUtils.uuid());//用户状态 未激0

    form.setState(0);//校验可能产生错误,来个Map存一下错误信息Maperrors = new HashMap();

    //用户名校验

    String username=form.getUname();

    if(username==null||username.trim().isEmpty()){

    errors.put("username", "用户名不能为空");

    }else if(username.length()<3||username.length()>10){

    errors.put("username", "用户名长度不合法");

    }

    //密码校验

    //邮箱校验

    if(errors.size()>0){

    //有错误,保存错误到regist.jsp中

    request.setAttribute("msg", errors);

    request.setAttribute("form", form);

    //请求转发

    return "f:/jsps/user/regist.jsp";

    }

    try {

    //异常处理

    biz.register(form);

    } catch (UserssException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    request.setAttribute("msg", e.getMessage());

    request.setAttribute("form", form);

    //请求转发

    return "f:/jsps/user/regist.jsp";

    }

    //5.发送邮件

    //读取配置文件email_template.properties的文件

    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 subject=props.getProperty("subject");//主题

    //收件人

    String to=form.getEmail();

    String content=props.getProperty("content");

    //使用uuid替换内容中的{0}

    content=MessageFormat.format(content, form.getUcode());

    //使用发邮件的小工具

    Session session=MailUtils.createSession(host, uname, pwd);

    //构建邮件需要的东西

    Mail mail = new Mail(from, to, subject, content);//创建邮件对象

    try {

    //发送邮件

    MailUtils.send(session, mail);

    } catch (MessagingException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    //邮件发送成功了

    request.setAttribute("msg", "恭喜你,注册成功了,请马上到邮箱进行激活!");

    return "f:/jsps/msg.jsp";

    }

    /**

    * 激活方法

    *

    * @param request

    * @param response

    * @return

    * @throws ServletException

    * @throws IOException

    */

    public String active(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    /**

    * 1.获取激活码

    * 2.调用biz地方法完成激活

    * 3.有错误保存错误信息

    * 4.跳转到指定页面

    */

    String code = request.getParameter("code");

    try {

    biz.active(code);

    request.setAttribute("msg", "恭喜你,激活成功了,可以去登录");

    } catch (UserssException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    request.setAttribute("msg", e.getMessage());

    }

    return "f:/jsps/msg.jsp";

    }

    /**

    * 登录方法

    *

    * @param request

    * @param response

    * @return

    * @throws ServletException

    * @throws IOException

    */

    public String login(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    /**

    * 1.封装表单数据到form中

    * 2.输入校验

    * 3.调用biz完成登录

    * 4.保存用户信息 ,存session中到处都可以用

    */

    Userss form=CommonUtils.toBean(request.getParameterMap(), Userss.class);

    try {

    //调用登陆方法

    Userss userss=biz.login(form);

    request.getSession().setAttribute("session_user", userss);

    request.getSession().setAttribute("cart", new Cart());

    return "r:/index.jsp";

    } catch (UserssException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    //出问题了

    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 quit(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    request.getSession().invalidate();//销毁session

    return "r:/index.jsp";

    }

    }

    相关文章

      网友评论

          本文标题:二、用户模块

          本文链接:https://www.haomeiwen.com/subject/grqidxtx.html