4、5----SSH綜合實(shí)戰(zhàn)(Struts+Spring+Hibernate)----我的智囊團(tuán)(用戶登錄1、2)
如果出現(xiàn)亂碼的解決方法把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>
驗(yàn)證碼:<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
// 登錄驗(yàn)證
public boolean login(User user) throws Exception;
// 找回密碼操作 --> 允許用戶修改密碼
public void updateUserpwd(String userid, String userpwd) throws Exception;
// 根據(jù)提示問題、答案、用戶名確定此用戶是否存在
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>
驗(yàn)證碼:<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 —— 我的智囊團(tuán)</title>
<META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">
<META NAME="Author" CONTENT="李興華">
<META NAME="Keywords" CONTENT="智囊團(tuán),SSH,tomcat,mysql">
<META NAME="Description" CONTENT="MLDN旗下網(wǎng)站 —— www.zhinangtuan.net.cn">
</head>
<body>
<center>
<jsp:include flush="true" page="../inc/template.jsp">
<jsp:param name="url" value="../"/>
</jsp:include>
<%-- 用戶已存在,可以進(jìn)行更新密碼操作 --%>
<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>
確認(rèn)密碼:<html:password property="confirmpwd"></html:password><br>
驗(yàn)證碼:<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 = "" 是為了清空那個表格中的內(nèi)容,不然會把上次的遺留下來
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之中設(shè)置內(nèi)容
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>
本節(jié)到此為止
posted on 2008-07-13 12:06 陳高杰 閱讀(677) 評論(0) 編輯 收藏 所屬分類: SSH我的智囊團(tuán)