用户登陆页面user_login.jsp对应action为login.do:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登陆</title>
<script type="text/javascript">
function confirmCodeNew(o){
o.src="<s:url action='confirm' namespace='/validation'/>";
}
</script>
</head> <body>
<form id="loginform" name="loginform" method="post" action="<s:url action='process'/>" onsubmit="return checkLoginForm();">
<table width="" height="" border="" cellpadding="" cellspacing="" class="table3">
<tr>
<td colspan=""><span class="font_red"><s:actionerror/></span></td>
</tr>
<tr>
<td width="" height="" align="right" valign="middle" class="font06">用户名:</td>
<td colspan="" align="left" valign="middle"><input type="text" name="username" id="username" tabindex="" /></td>
</tr>
<tr>
<td width="" height="" align="right" valign="middle" class="font06">密 码:</td>
<td colspan="" align="left" valign="middle"><input type="password" name="pwd" id="pwd" tabindex="" /></td>
</tr>
<tr>
<td width="" height="" align="right" valign="middle" class="font06">验证码:</td>
<td width="" align="left" valign="middle"><input name="confirm_code" type="text" class="table3" size="" maxlength="" /></td>
<td width=""><img src="<s:url action='confirm' namespace='/validation'/>" onclick="confirmCodeNew(this)" style="cursor:pointer" /></td>
<td width="" align="left" valign="middle" class="font06"> </td>
</tr>
<tr>
<td height=""> </td>
<td colspan="" align="left" valign="middle"><input type="checkbox" name="rember_me" onclick="remeber_me_change(this);"/>
<span class="font06">记住用户名</span></td>
</tr>
<tr>
<td height=""> </td>
<td height="" colspan="" align="left" valign="middle"><img src="<s:url value='/images/3vsxdt_36.gif'/>" onclick="checkLoginForm()" style="cursor:pointer" width="" height="" border="" /></td>
</tr>
</table>
</form>
</body>
</html>
用户登陆类(UserLogin.java):
package com.web.struts.action; import javax.servlet.http.Cookie; import org.apache.commons.lang.StringUtils; import commodel.Member;
import com.service.IMemberService;
import com.tools.Constant;
import com.tools.MD5Bean; /**
* title:用户登陆
*
* @author conkeyn
* @时间 2009-3-24:上午10:06:25
*/
public class UserLogin extends BaseAction { /** */
private static final long serialVersionUID = -8059803575427183277L; private Member member; public void setMemberService(IMemberService memberService) {
this.memberService = memberService;
} private IMemberService memberService; /**
* 退出
*
* @return
*/
public String logout() {
getSession().removeAttribute(Constant.MEMBER_SESSION_ID);
Cookie[] cookies = getRequest().getCookies();
Cookie member_cookie = null;
for (int i = 0; i < cookies.length; i++) {
member_cookie = cookies[i];
if (member_cookie != null
&& StringUtils.isNotBlank(member_cookie.getName())
&& StringUtils.isNotBlank(member_cookie.getValue())
&& member_cookie.getName().equals(
Constant.MEMBER_SESSION_ID)) {
member_cookie.setMaxAge(-1);// 设置有效期为-1秒,浏览器会自动删除
getResponse().addCookie(member_cookie);
break;
}
}
return SUCCESS;
} /**
* 用户登陆
*
* @return
*/
public String login() { return "login";
} /**
* 处理用户登陆
*
* @return
*/
public String process() {
String confirm_code = getRequest().getParameter("confirm_code");
String confirmcode = (String) getSession().getAttribute("confirmcode");
if (StringUtils.isNotBlank(confirm_code)
&& StringUtils.isNotBlank(confirmcode)
&& confirm_code.equals(confirmcode)) {
String username = getRequest().getParameter("username");
String pwd = getRequest().getParameter("pwd");
String rember_me = getRequest().getParameter("rember_me");
MD5Bean md5 = new MD5Bean();
pwd = md5.MD5Encrypt(pwd);
member = memberService.getMember(username);
if (member == null) {
this.addActionError("不存在该用户,请注册后再登陆!");
return ERROR;
} else if (!pwd.equalsIgnoreCase(member.getPwd())) {
this.addActionError("密码错误,请重新输入!");
return ERROR;
}
getSession().setAttribute(Constant.MEMBER_SESSION_ID,
member.getUsername());
// 设置cookie
if (StringUtils.isNotBlank(rember_me)
&& rember_me.equalsIgnoreCase("true")) {
Cookie cookie = new Cookie(Constant.MEMBER_SESSION_ID, member
.getUsername());
cookie.setMaxAge(60 * 60 * 24 * 365);
getResponse().addCookie(cookie);
}
member.setLoged(true);
memberService.saveMember(member);
} else {
this.addActionError("验证码错误,请重新输入!");
return ERROR;
}
return SUCCESS;
} public Member getMember() {
return member;
} public void setMember(Member member) {
this.member = member;
} }
Struts配置文件(struts-login.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<package name="login" extends="struts-default">
<!-- 登陆 -->
<action name="login" class="memberAction" method="login">
<result name="login" type="dispatcher"> /WEB-INF/pages/userlogin.jsp
</result>
</action>
<action name="logout" class="memberAction" method="logout">
<result name="success" type="redirectAction">login</result>
</action>
<action name="process" class="memberAction" method="process">
<result name="error" type="dispatcher">/WEB-INF/pages/userlogin.jsp
</result>
<result name="success" type="dispatcher">/WEB-INF/pages/userlogin_msg.jsp
</result>
</action>
</package>
</struts>