web 项目中一般在登录的时候都会用到记住密码功能。
1.jsp页面:
01
<% String flag = (String)session.getAttribute(
"flag"
)==
null
?
""
:(String)session.getAttribute(
"flag"
);
06
Cookie[] cookies=request.getCookies();
08
for
(
int
i=
0
;i<cookies.length;i++){
09
if
(cookies[i].getName().equals(
"cookie_user"
)){
10
String value = cookies[i].getValue();
11
if
(value!=
null
&&!
""
.equals(value)){
12
name=cookies[i].getValue().split(
"-"
)[
0
];
13
if
(cookies[i].getValue().split(
"-"
)[
1
]!=
null
&& !cookies[i].getValue().split(
"-"
)[
1
].equals(
"null"
)){
01
password=cookies[i].getValue().split(
"-"
)[
1
];
06
request.setAttribute(
"name"
,name);
07
request.setAttribute(
"passward"
,password);
15
<div id=
"logo"
><img src=
"<%=request.getContextPath() %>/frontsite/Images/Logo.jpg"
width=
"244"
height=
"44"
alt=
""
/></div>
17
<form action=
"<%=request.getContextPath() %>/frontsite/login.do?method=login"
method=
"post"
id=
"forms"
onsubmit=
"checkForm();return false;"
>
20
<li
class
=
"l1"
>用户名:</li>
21
<li
class
=
"l2"
><input name=
"login_id"
type=
"text"
id=
"login_id"
class
=
"input"
value=
"<%=name %>"
/></li>
22
<li
class
=
"l3"
>* 最大限度为
20
字节</li>
23
<li
class
=
"l1"
>密码:</li>
24
<li
class
=
"l2"
><input name=
"login_pwd"
type=
"password"
id=
"login_pwd"
class
=
"input"
value=
"<%=password %>"
/></li>
25
<li
class
=
"l3"
>* 最大限度为
20
字节</li>
28
<li
class
=
"l2"
style=
"width: 180px;"
><input name=
"checkImg"
id=
"checkImg"
type=
"text"
class
=
"input"
style=
"width: 60px;"
size=
"10"
/>
29
<img src=
"<%=request.getContextPath() %>/CheckImg_FT"
width=
"49"
height=
"22"
/></li>
32
<label><input type=
"checkbox"
name=
"flag"
id=
"flag"
value=
"1"
<%
if
(flag!=
null
&& flag.equals(
"1"
)){%> checked ; value =
"1"
; <%}
else
{%> value=
"0"
<%;}%> />记住密码</label>
33
<label><span style=
"margin-left:10px; color: #F00;"
><html:errors /></span></label>
36
<li
class
=
"l4"
><input type=
"image"
name=
"imageField"
id=
"imageField"
src=
"<%=request.getContextPath() %>/frontsite/Images/login_bnt.jpg"
/></li>
2.java 类:
01
String flag = request.getParameter(
"flag"
);
03
if
(flag!=
null
&& flag.equals(
"1"
)){
04
Cookie cookie =
new
Cookie(
"cookie_user"
, po.getLogin_id()+
"-"
+form.getLogin_pwd());
05
cookie.setMaxAge(
60
*
60
*
24
*
30
);
06
response.addCookie(cookie);
08
Cookie cookie =
new
Cookie(
"cookie_user"
,po.getLogin_id()+
"-"
+
null
);
09
cookie.setMaxAge(
60
*
60
*
24
*
30
);
10
response.addCookie(cookie);