Struts中使用Cookie控制登錄的流程
讀取 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);
posted on 2006-10-18 17:20 白洋 閱讀(1644) 評論(2) 編輯 收藏 所屬分類: Struts 知識點滴