读取
Cookie
集合,遍历集合找到所需的
Cookie
,如果找到保存进
Session
,否则跳转到登录页面;
所需资源:
ReadCookieAction.java-
读取
Cookie
集合,如果找到对应
Cookie
,写入
Session
;
Login.jsp-
登录用页面;
LoginSubmit.java-
记录登录信息,并写入
Session
;
LoginOk.jsp-
读取
Session
,并显示。
读
Cookie
的方法
:
Cookie[] cookies = request.getCookies();
if
(cookies !=
null
) {
for
(
int
i=0; i<cookies.
length
; i++) {
Cookie cookie = cookies[i];
if
(cookie.getName().equals(
"userInfo"
)) {
String value = cookie.getValue();
String[] info = value.split(
"_"
);
UserForm userForm =
new
UserForm();
userForm.setUserName(info[0]);
userForm.setUserPassword(info[1]);
request.getSession().setAttribute(
"userForm"
, userForm);
return
mapping.findForward(
"ok"
);
}
}
}
写
Cookie
的方法:
Cookie c =
new
Cookie(
"userInfo"
,userForm.getUserName()+
"_"
+userForm.getUserPassword());
c.setComment(
"A test cookie"
);
c.setMaxAge(120);
response.addCookie(c);