陈高杰

kingaragorn

常用链接

统计

最新评论

4、5----SSH综合实战(Struts+Spring+Hibernate)----我的智囊团(用户登录1、2)

如果出现乱码的解决方法
把my.ini里的两个default-character-set=latin1都改为gbk

login.jsp

<body>
<center>
<jsp:include flush="true" page="../inc/template.jsp">
    <jsp:param name="url" value="../"/>
</jsp:include>

<logic:present name="flag" scope="request">
    <logic:equal value="error" name="flag" scope="request">
        <h2>登陆失败,错误的用户名或密码!</h2>
    </logic:equal>
</logic:present>

<html:form action="jsp/user.do" method="post">
    用户ID:<html:text property="userid"></html:text><br>
    登陆密码:<html:password property="userpwd"></html:password><br>
    验证码:<html:text property="checkcode"></html:text>
        <img src="image.jsp">
    <br>
    <input type="hidden" name="status" value="login">
    <input type="hidden" name="type" value="2">
    <html:submit value="登陆"></html:submit>
    <html:reset value="重置"></html:reset>
    <br>
    <a href="register.jsp">注册新用户?</a>
    <a href="forgetpwd.jsp">忘记密码?</a>
</html:form>
</center>
</body>

IUserDAO.java

    // 登录验证
    public boolean login(User user) throws Exception;
   
    // 找回密码操作 --> 允许用户修改密码
    public void updateUserpwd(String userid, String userpwd) throws Exception;

    // 根据提示问题、答案、用户名确定此用户是否存在
    public boolean isExists(String userid, String userques, String userans)
            throws Exception;   

IUserDAOImpl.java

    public boolean isExists(String userid, String userques, String userans)
            throws Exception {
        boolean flag = false;
        String hql = "FROM User AS u WHERE u.userid=? AND u.userques=? AND u.userans=?";
        Query q = super.getSession().createQuery(hql);
        q.setString(0, userid);
        q.setString(1, userques);
        q.setString(2, userans);
        List all = q.list();
        if (all.size() > 0) {
            flag = true;
        }
        return flag;
    }

    public boolean login(User user) throws Exception {
        boolean flag = false;
        String hql = "FROM User AS u WHERE u.userid=? and u.userpwd=?";
        Query q = super.getSession().createQuery(hql);
        q.setString(0, user.getUserid());
        q.setString(1, user.getUserpwd());
        List all = q.list();
        if (all.size() > 0) {
            flag = true;
            User t = (User) all.get(0);
            user.setGrade(t.getGrade());
        }
        return flag;
    }

    public void updateUserpwd(String userid, String userpwd) throws Exception {
        String hql = "UPDATE User SET userpwd=? WHERE userid=?";
        Query q = super.getSession().createQuery(hql);
        q.setString(0, userpwd);
        q.setString(1, userid);
        q.executeUpdate();
    }

forgetpwd.jsp

<body>
<center>
<jsp:include flush="true" page="../inc/template.jsp">
    <jsp:param name="url" value="../"/>
</jsp:include>
<html:form action="jsp/user.do" method="post">
    用户ID:<html:text property="userid"></html:text><br>
    找回密码提示问题:<html:text property="userques"></html:text><br>
    密码提示问题答案:<html:text property="userans"></html:text><br>
    验证码:<html:text property="checkcode"></html:text><img src="image.jsp"><br>
    <input type="hidden" name="status" value="forgetpwd">
    <input type="hidden" name="type" value="3">
    <html:submit value="找回密码"></html:submit>
    <html:reset value="重置"></html:reset>
    <br>
    <a href="login.jsp">用户登陆?</a>
    <a href="register.jsp">用户注册?</a>
</html:form>
</center>
</body>

updatepwd.jsp

<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
    <title>MLDN —— 我的智囊团</title>
    <META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
    <META NAME="Author" CONTENT="李兴华">
    <META NAME="Keywords" CONTENT="智囊团,SSH,tomcat,mysql">
    <META NAME="Description" CONTENT="MLDN旗下网站 —— www.zhinangtuan.net.cn">
</head>
<body>
<center>
<jsp:include flush="true" page="../inc/template.jsp">
    <jsp:param name="url" value="../"/>
</jsp:include>
<%-- 用户已存在,可以进行更新密码操作 --%>
<logic:present name="flag" scope="request">
<logic:equal value="exists" name="flag" scope="request">
    <html:form action="jsp/user.do" method="post">
        新的密码:<html:password property="userpwd"></html:password><br>
        确认密码:<html:password property="confirmpwd"></html:password><br>
        验证码:<html:text property="checkcode"></html:text><img src="image.jsp"><br>
        <input type="hidden" name="status" value="updatepwd">
        <input type="hidden" name="type" value="4">
        <input type="hidden" name="userid" value="${param.userid}">
        <html:submit value="更新密码"></html:submit>
        <html:reset value="重置"></html:reset>
        <br>
        <a href="login.jsp">用户登陆?</a>
        <a href="register.jsp">用户注册?</a>
    </html:form>
    <script language="javaScript">
        document.userForm.checkcode.value = "" ;
    </script>
   
</logic:equal>
</logic:present>
</center>
</body>
</html:html>

注:document.userForm.checkcode.value = "" 是为了清空那个表格中的内容,不然会把上次的遗留下来

updatepwd_do.jsp

<body>
<center>
<jsp:include flush="true" page="../inc/template.jsp">
    <jsp:param name="url" value="../"/>
</jsp:include>
<h2>密码修改成功!</h2>
<a href="login.jsp">用户登陆?</a>
<a href="register.jsp">用户注册?</a>
</center>
</body>

UserForm.java

    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (type == 1) {
            if (this.userid == null || "".equals(this.userid)) {
                errors.add("userid", new ActionMessage("user.userid.null"));
            }
            if (this.userpwd == null || "".equals(this.userpwd)) {
                errors.add("userpwd", new ActionMessage("user.userpwd.null"));
            } else {
                if (!this.userpwd.equals(this.confirmpwd)) {
                    errors.add("confirmpwd", new ActionMessage("user.confirmpwd.error"));
                }
            }
            if (this.userques == null || "".equals(this.userques)) {
                errors.add("userques", new ActionMessage("user.userques.null"));
            }
            if (this.userans == null || "".equals(this.userans)) {
                errors.add("userans", new ActionMessage("user.userans.null"));
            }
            if (this.checkcode == null || "".equals(this.checkcode)) {
                errors.add("checkcode", new ActionMessage("checkcode.null"));
            }
        }
        if (type == 2) {
            if (this.userid == null || "".equals(this.userid)) {
                errors.add("userid", new ActionMessage("user.userid.null"));
            }
            if (this.userpwd == null || "".equals(this.userpwd)) {
                errors.add("userpwd", new ActionMessage("user.userpwd.null"));
            }
            if (this.checkcode == null || "".equals(this.checkcode)) {
                errors.add("checkcode", new ActionMessage("checkcode.null"));
            }           
        }
        if (type == 3) {
            if (this.userid == null || "".equals(this.userid)) {
                errors.add("userid", new ActionMessage("user.userid.null"));
            }
            if (this.userques == null || "".equals(this.userques)) {
                errors.add("userques", new ActionMessage("user.userques.null"));
            }
            if (this.userans == null || "".equals(this.userans)) {
                errors.add("userans", new ActionMessage("user.userans.null"));
            }
            if (this.checkcode == null || "".equals(this.checkcode)) {
                errors.add("checkcode", new ActionMessage("checkcode.null"));
            }
        }
        if (type == 4) {
            if (this.userid == null || "".equals(this.userid)) {
                errors.add("userid", new ActionMessage("user.userid.null"));
            }
            if (this.userpwd == null || "".equals(this.userpwd)) {
                errors.add("userpwd", new ActionMessage("user.userpwd.null"));
            } else {
                if (!(this.userpwd.equals(this.confirmpwd))) {
                    errors.add("configpwd", new ActionMessage(
                            "user.confirmpwd.error"));
                }
            }
            if (this.checkcode == null || "".equals(this.checkcode)) {
                errors.add("checkcode", new ActionMessage("checkcode.null"));
            }
        }       
        return errors;
    }


UserAction.java

    public ActionForward login(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;
        String ccode = (String) request.getSession().getAttribute("ccode");
        String checkcode = userForm.getCheckcode();
        if (!(checkcode.equals(ccode))) {
            ActionMessages errors = new ActionMessages();
            errors.add("checkcode", new ActionMessage("checkcode.error"));
            super.saveErrors(request, errors);
            return mapping.getInputForward();
        }
        User user = new User();
        MD5Code md5 = new MD5Code();
        user.setUserid(userForm.getUserid());
        user.setUserpwd(md5.getMD5ofStr(userForm.getUserpwd()));
        boolean flag = false;
        try {
            flag = this.iuserdao.login(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag) {
            // 登陆成功
            // 向session之中设置内容
            request.getSession().setAttribute("userid", user.getUserid());
            request.getSession().setAttribute("grade", user.getGrade());
            return mapping.findForward("loginsuccess");
        } else {
            request.setAttribute("flag", "error");
            return mapping.findForward("loginfailure");
        }
    }

    public ActionForward forgetpwd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;
        String ccode = (String) request.getSession().getAttribute("ccode");
        String checkcode = userForm.getCheckcode();
        if (!(checkcode.equals(ccode))) {
            ActionMessages errors = new ActionMessages();
            errors.add("checkcode", new ActionMessage("checkcode.error"));
            super.saveErrors(request, errors);
            return mapping.getInputForward();
        }
        boolean flag = true;
        try {
            flag = this.iuserdao.isExists(userForm.getUserid(), userForm
                    .getUserques(), userForm.getUserans());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回到修改密码页
        // 返回到提示问题页
        if (flag) {
            request.setAttribute("flag", "exists");
            return mapping.findForward("exists");
        } else {
            return mapping.findForward("notexists");
        }
    }

    public ActionForward updatepwd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;
        String ccode = (String) request.getSession().getAttribute("ccode");
        String checkcode = userForm.getCheckcode();
        if (!(checkcode.equals(ccode))) {
            ActionMessages errors = new ActionMessages();
            errors.add("checkcode", new ActionMessage("checkcode.error"));
            super.saveErrors(request, errors);
            return mapping.getInputForward();
        }
        MD5Code md5 = new MD5Code();
        try {
            this.iuserdao.updateUserpwd(userForm.getUserid(), md5
                    .getMD5ofStr(userForm.getUserpwd()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapping.findForward("updatepwddo");
    }

struts-config.xml

    <action
      attribute="userForm"
      input="/jsp/errors.jsp"
      name="userForm"
      parameter="status"
      path="/jsp/user"
      scope="request"
      type="org.lxh.zngt.struts.action.UserAction">
      <forward name="registersuccess" path="/jsp/index.jsp"></forward>
      <forward name="registerfailure" path="/jsp/register.jsp"></forward>
      <forward name="loginsuccess" path="/jsp/index.jsp"></forward>
      <forward name="loginfailure" path="/jsp/login.jsp"></forward>
      <forward name="exists" path="/jsp/updatepwd.jsp"></forward>
      <forward name="notexists" path="/jsp/forgetpwd.jsp"></forward>
      <forward name="updatepwddo" path="/jsp/updatepwd_do.jsp"></forward>
    </action>

本节到此为止

posted on 2008-07-13 12:06 陈高杰 阅读(658) 评论(0)  编辑  收藏 所属分类: SSH我的智囊团


只有注册用户登录后才能发表评论。


网站导航: