public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
LazyValidatorForm userForm = (LazyValidatorForm) form;
log.info("UserAction:验证用户登录");
//得到用户名和密码
String username = (String) userForm.get("username");
String password = (String) userForm.get("password");
Map<String,Object> userMap = new HashMap<String,Object>();
userMap.put("userName", username);
userMap.put("userPassword", password);
//设置Cookie
int cookieDate = Integer.parseInt(userForm.get("cookieDate").toString());
if(cookieDate != 0)
{
Cookie cookie1 = new Cookie("username",username);
Cookie cookie2 = new Cookie("password",password);
if(cookieDate == 1)
{
//一天
cookie1.setMaxAge(60 * 60 * 24);
cookie2.setMaxAge(60 * 60 * 24);
}
if(cookieDate == 2)
{
//一个月
cookie1.setMaxAge(60 * 60 * 24 * 31);
cookie2.setMaxAge(60 * 60 * 24 * 31);
}
if(cookieDate == 3)
{
//一年
cookie1.setMaxAge(60 * 60 * 24 * 365);
cookie2.setMaxAge(60 * 60 * 24 * 365);
}
//把捕获来的cookies添加到响应里
response.addCookie(cookie1);
response.addCookie(cookie2);
}
User user = null;
//验证用户名和密码
if(userManager.createCriteria(Restrictions.allEq(userMap)).list().size() > 0)
{
log.info("UserAction:登录成功");
user = (User) userManager.createCriteria(Restrictions.allEq(userMap)).list().get(0);
request.getSession().setAttribute("user", user);
return mapping.findForward("success");
}
else
{
this.saveMessage(request, "login.error", "用户名或密码错误");
log.info("UserAction:登录失败");
request.setAttribute("error","username or password is wrong");
return mapping.getInputForward();
}
}登录前的跳转:
/** */
/**
* 登录前的跳转
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public
ActionForward forwardlogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
log.info("UserAction:登录前的跳转,Cookie处理");
//取出cookies
Cookie[] cookies = request.getCookies();
String username = null;
String password = null;
if(cookies.length != 0)
{
for(int i = 0; i < cookies.length; i ++)
{
//根据key取出值
if(cookies[i].getName().equals("username"))
{
username = cookies[i].getValue();
}
if(cookies[i].getName().equals("password"))
{
password = cookies[i].getValue();
}
}
}
request.setAttribute("username", username);
request.setAttribute("password", password);
return mapping.findForward("login");
}
前台:
<
form
action
="${ctx}/user.do?method=login"
method
="post"
onsubmit
="return check();"
>
<
table
cellpadding
=0
cellspacing
=0
border
=0
width
=98%
bgcolor
=#0099cc
align
=center
>
<
tr
>
<
td
>
<
table
cellpadding
=6
cellspacing
=1
border
=0
width
=100%
>
<
tr
>
<
td
bgcolor
=#99ccff
valign
=middle
colspan
=2
align
=center
>
<!--
判断用户名或密码是否错误
-->
<
logic:empty
name
="error"
scope
="request"
>
<
font
color
="green"
><
b
>
请输入您的用户名、密码登陆
</
b
></
font
>
</
logic:empty
>
<
logic:notEmpty
name
="error"
scope
="request"
>
<
bean:message
key
="login.error"
/>
</
logic:notEmpty
>
</
td
>
</
tr
>
<
tr
>
<
td
bgcolor
=#f2f8ff
valign
=middle
>
请输入您的用户名
</
td
>
<
td
bgcolor
=#f2f8ff
valign
=middle
>
<
INPUT
value
="${requestScope.username}"
name
="username"
type
=text
id
="username"
onblur
="return check();"
>
<
a
href
="${ctx}/main/common/reg.jsp"
>
没有注册?
</
a
>
</
td
>
<!--
用户名错误提示信息
-->
<
td
id
="username_info"
style
="color:red;position: absolute;left:550px;top:235px;"
></
td
>
</
tr
>
<
tr
>
<
td
bgcolor
=#f2f8ff
valign
=middle
>
请输入您的密码
</
td
>
<
td
bgcolor
=#f2f8ff
valign
=middle
>
<
INPUT
value
="${requestScope.password}"
name
="password"
type
=password
id
="password"
onblur
="return check();"
>
<
a
href
="lostpass.jsp"
>
忘记密码?
</
a
>
</
td
>
<!--
密码错误提示信息
-->
<
td
id
="password_info"
style
="color:red;position: absolute;left:550px;top:270px;"
></
td
>
</
tr
>
<
tr
>
<
td
bgcolor
=#f2f8ff
valign
=top
width
=30%
>
<
b
>
Cookie 选项
</
b
>
<
BR
>
请选择您的Cookie保存时间
<
br
>
下次访问可以方便输入
<
br
><
br
/>
<
a
href
="#"
onclick
="clearCookie();"
style
="color:green;"
>
清空Cookies
</
a
>
<
div
id
="cookie_info"
style
="color:maroon;position: absolute;left:100px;top:360px;"
></
div
>
</
td
>
<
td
bgcolor
=#f2f8ff
valign
=middle
>
<
input
type
="radio"
id
="cookieDate"
name
="cookieDate"
value
="0"
checked
>
不保存,关闭浏览器就失效
<
br
>
<
input
type
="radio"
id
="cookieDate"
name
="cookieDate"
value
="1"
>
保存一天
<
br
>
<
input
type
="radio"
id
="cookieDate"
name
="cookieDate"
value
="2"
>
保存一月
<
br
>
<
input
type
="radio"
id
="cookieDate"
name
="cookieDate"
value
="3"
>
保存一年
<
br
>
</
td
>
</
tr
>
<
input
type
=hidden
name
=comeURL
value
="#"
/>
<
tr
>
<
td
bgcolor
=#99ccff
valign
=middle
colspan
=2
align
=center
>
<
input
type
=submit
name
="submit"
value
="登 陆"
>
</
td
>
</
tr
>
</
co
分享到:
评论