Warning: Creating default object from empty value in /www/wwwroot/linuxsight.com/wp-content/themes/lensnews/admin/ReduxCore/inc/class.redux_filesystem.php on line 29
我的SSH项目之旅(4.用户登陆及密码操作) _ Linux视野

JAVA

我的SSH项目之旅(4.用户登陆及密码操作)

LensNews

如果出现mysql乱码问题 修改my.ini两处红色的地方gb2312

[client]



port=3306



[mysql]



default-character-set=gb2312





# SERVER SECTION

# ----------------------------------------------------------------------

#

# The following options will be read by the MySQL Server. Make sure that

# you have installed the server correctly (see above) so it reads this

# file.

#

[mysqld]



# The TCP/IP Port the MySQL Server will listen on

port=3306





#Path to installation directory. All paths are usually resolved relative to this.

basedir="E:/compiler/mysql/"



#Path to the database root

datadir="E:/compiler/mysql/Data/"



# The default character set that will be used when a new schema or table is

# created and no character set is defined

default-character-set=gb2312







登陆模块

可以仿照注册模块,所以问题不多,出现的错误主要都是页面呈现不出,也就是struts标签的错误,一点小差错就会导致页面呈现不出,我想也只能对照正确的页面看自己到底有什么写错了

UserDAOImpl.java

  public boolean login(User user) throws Exception {
    // TODO Auto-generated method stub
    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 l=q.list();
    if(l.size()>0){
      flag=true;
      User u=(User)l.get(0);
      user.setGrade(u.getGrade());取得等级
     
    }
    return flag;

  }






UserAction.java
public ActionForward login(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) {
    UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
// 首先还是验证输入验证码是否正确
  String ccode = (String) request.getSession().getAttribute("ccode");
  String checkcode = userForm.getCheckcode();
  if (!(ccode.equals(checkcode))) {
      ActionMessages errors = new ActionMessages();
      errors.add("ccode", new ActionMessage("checkcode.error"));
      super.saveErrors(request, errors);
      return mapping.getInp
utForward();

    }
    User user=new User();
    MD5Code mc = new MD5Code();
    user.setUserid(userForm.getUserid());
    user.setUserpwd(mc.getMD5ofStr(userForm.getUserpwd()));
  boolean flag=false;
  try {
  flag=this.userdao.login(user);
  if(flag){
  request.getSession().setAttribute("userid", user.getUserid());
  request.getSession().setAttribute("grade", user.getGrade());
  return mapping.findForward("loginsuc");
  }else{
  request.setAttribute("flag", "error");
  return mapping.findForward("loginfail");
  }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return mapping.findForward("loginfail");
     
    }

  }



login.jsp   依然利用模板建立

<body>
<center>
<jsp:include flush="true" page="../inc/template.html"/>

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

<h1>用户登陆</h1>
<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>



接下来是找回密码和更新密码模块

UserDAO.java接口

// 找回密码 允许用户修改密码
public void updatepwd(String userid,String userpwd) throws Exception;
// 根据提示问题 答案 用户ID 确认用户是否存在
public boolean isExists(String userid, String userques, String userans)

      throws Exception;



UserDAOImpl.java 实现这样两个方法

public boolean isExists(String userid, String userques, String userans)
      throws Exception {
    // TODO Auto-generated method stub
    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 l=q.list();
    if(l.size()>0){
      flag=true;
     
        }
    return flag;
  }

  public void updatepwd(String userid,String userpwd) throws Exception {
    // TODO Auto-generated method stub
    String hql="update User set userpwd=? where userid=?";
    Query q=super.getSession().createQuery(hql);
    q.setString(0, userpwd);
    q.setString(1, userid);
    q.executeUpdate();

  }

UserAction.java

  public ActionForward forget(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) {
    UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
    boolean flag = false;
    try {
      flag = this.userdao.isExists(userForm.getUserid(), userForm
            .getUserques(), userForm.getUserans());
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    if(flag){

//设置一个flag值保存在request范围让jsp页面做判断输出应有的信
      request.setAttribute("flag", "true");

      return mapping.findForward("exist");
     
    }else{
      request.setAttribute("flag", "false");
      return mapping.findForward("notexist");
     
    }
   
  }

  public ActionForward updatepwd(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) {
    UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
    MD5Code m=new MD5Code();
    String userid=userForm.getUserid();
    String userpwd=m.getMD5ofStr(userForm.getUserpwd());
    try {
      this.userdao.updatepwd(userid, userpwd);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return mapping.findForward("updatepwddo");

  }



forgetpwd.jsp

<h2>忘记密码</h2>
<%-- 用户不存在或问题或答案错误--%>
<logic:present name="flag" scope="request">
<logic:equal value="false" name="flag">
<h3>用户不存在或问题或答案错误</h3>
</logic:equal>
</logic:present>
<html:form ac

tion="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="forget"><br>
<input type="hidden" name="type" value="3"><br>
<html:submit value="找回密码"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
加入这段javascript脚本可以让验证码在返回的时候留空
  <script language="javaScript">
    document.userForm.checkcode.value = "" ;

  </script>



Updatepwd.jsp

<logic:present name="flag" scope="request">
<logic:equal value="true" name="flag" scope="request">
<h2>更新密码</h2>
<html:form action="jsp/user.do">
新的密码:<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>





欢迎fantlam光临 头衔:开发爱好者

index.jsp

<logic:present name="userid" scope="session">
欢迎${userid}光临
头衔:<%=IntegralGrade.getInstance().getGradeInfo((Integer)session.getAttribute("grade")) %>

</logic:present>


IntegralGrade类就不写了

往下层目录建立JSP页面的时候出现路径问题
<jsp:include flush="true" page="../../inc/template.jsp">
<jsp:param name="url" value="../../" />
通过传参方式让不同层次目录的JSP根据自己的情况找到模板上制定的路径
<jsp:param name="image" value="../"/>
</jsp:include>

template.jsp中相关代码
<img src="${param.url}images/banner_logo.gif" alt="fantlam"

src="${param.url}images/banner_ico06.jpg" alt="我要提问" border="0">
   
<a href="${param.image}login.jsp">
src="${param.url}images/banner_ico05.jpg" alt="我要登录" border="0">
(0)

本文由 Linux视野 作者:fantlam 发表,转载请注明来源!

关键词:, , , ,
LensNews

热评文章

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注